Create "DEFINE" at runtime in code?

57 views
Skip to first unread message

BP

unread,
Mar 24, 2013, 8:30:10 PM3/24/13
to haxe...@googlegroups.com
Hey there.... Is there anyway I can "define" something in code, and not in a project file (hxml or nmml)?

I've seen this:

http://haxe.org/ref/conditionals

and I get that I can do stuff like this:

#if mydebug 
 trace("Muy Debug infos for all debug compiles"); 
#end

but how do I do define a new define in a define?

e.g.

#if mydebug 
 #define another_option
#end

then later use it like so:

#if another_option 
 ....
#end

Thanks!

Greg Dove

unread,
Mar 24, 2013, 8:47:29 PM3/24/13
to haxe...@googlegroups.com
It looks like there should be a way to do this with macros, but I have not yet tried, so can't speak from experience.

static function define( flag : String, ?value : String ) : Void




--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

BP

unread,
Mar 24, 2013, 9:40:44 PM3/24/13
to haxe...@googlegroups.com
I took a look at that, and it seems to only work for Neko... I tried it for Flash for example, and it fails with:

source/Player.hx:34: characters 6-21 : haxe.macro.#Compiler has no field define

and in the docs it say "available in Neko" only... but that definitely looked like a great solution... but I think it only works for Neko.....

Jason O'Neil

unread,
Mar 24, 2013, 10:29:22 PM3/24/13
to haxe...@googlegroups.com
Hi Brad

The conditionals you mentioned are a compile-time feature - if you look at the source code output (in JS, PHP, AS3 source etc) you'll notice that entire lines etc. are missing - the "#if mydebug" stuff is all decided by the Haxe compiler, and only the defined options make it in to the source code.

So there's no way to do these "at runtime".  What Greg Dove mentioned is doing it inside a macro, which again is at compile-time.  (In case you're not familiar with macros, have a read here).  Now macros use the neko API, which is why the haxe.macro.* classes say they're only available on neko.  In reality, they're only available in a macro, but they work in macros no matter if you're targetting Flash or Neko or JS or anything.

Anyway, after saying all of that, the solution is here (also attached):

https://gist.github.com/jasononeil/5234547

How it works:
  • When you compile, you use "-D mydebug"
  • When you compile, you use "--macro MacroDefine.setDefines()" to tell the compiler to run your macro before compiling.
  • MacroDefine.setDefines() will set new defines, based on your current ones
  • In your main code, you can use #if conditionals for any defines that you set in setDefines()
Hope that is helpful!

I've tested it with Haxe3RC, but it should be the same for Haxe 2.10. 

Jason
MacroDefine.hx

BP

unread,
Mar 25, 2013, 8:36:14 AM3/25/13
to haxe...@googlegroups.com
Thanks! that looks great.... thanks for your help....
Reply all
Reply to author
Forward
0 new messages