Dereferencing custom variables?

102 views
Skip to first unread message

Zdravko Beykov

unread,
Feb 4, 2014, 8:50:31 AM2/4/14
to haxe...@googlegroups.com
Hi guys, is there a way to dereference defines in your .hx code?

Like for example if I have <set name="customvar" value="blabla"/> in project.xml , how can I (if I can at all), do something like trace( $customvar ), to get "blabla"

Yours,
Ozdy

Dan Korostelev

unread,
Feb 4, 2014, 9:53:47 AM2/4/14
to haxe...@googlegroups.com
One (or even the only?) way to do it is with macro, like that:

class Main
{
    static function main()
    {
        trace(getMyDef());
    }

    static macro function getMyDef()
    {
        return macro $v{haxe.macro.Context.definedValue("mydef")};
    }
}


вторник, 4 февраля 2014 г., 17:50:31 UTC+4 пользователь Zdravko Beykov написал:

dlots

unread,
Feb 4, 2014, 2:49:26 PM2/4/14
to haxe...@googlegroups.com
An explanation with regards to if this is logically feasible with a shorthand at runtime would be nice.

With regards to accomplishing the same thing at runtime effectively, would be to wrap dereferenced variables in objects. Pretty annoying to do for local variables though.

Dan Korostelev

unread,
Feb 4, 2014, 4:02:42 PM2/4/14
to haxe...@googlegroups.com
I can't understand what are you talking about, but that example has no runtime overhead if you're talking about that. Macro call is replaced by a value of "mydef".

вторник, 4 февраля 2014 г., 23:49:26 UTC+4 пользователь dlots написал:

dlots

unread,
Feb 5, 2014, 2:43:00 AM2/5/14
to haxe...@googlegroups.com
I'm talking about a shorthand for reflection on a local variable. Something that's generally available only scripting/interpreted languages.

var test:Int=0;
var prop:String="test";
trace(&prop);

Not really possible unless you presumably auto wrap test into an object and execute Reflect.field on object. 

Luca

unread,
Feb 5, 2014, 3:07:10 AM2/5/14
to haxe...@googlegroups.com
How on earth does that have anything to do with the original question regarding -D defines?

dlots

unread,
Feb 5, 2014, 8:34:51 AM2/5/14
to haxe...@googlegroups.com
lol, it doesn't. i just sort of blurted it out.

Carlos Madrazo

unread,
Mar 10, 2014, 11:01:57 AM3/10/14
to haxe...@googlegroups.com
Hi. Thanks for this. First I tried it but I got 
"You cannot access the flash package while in a macro (for flash.display.Bitmap)" on the getMyDef() line
:D

On Tuesday, February 4, 2014 9:53:47 AM UTC-5, Dan Korostelev wrote:
Reply all
Reply to author
Forward
0 new messages