Property extraction macro

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Property extraction macro

zOOn
Yesterday I made an unpleasant discovery: Haxe cannot Reflect properties!
How can I tween alpha property in the Matt Tuttle port of FlashPunk? :)

Today I wrote a property extraction macro, it accepts some object's field or
property and returns anonymous object with getter and setter closures.


var someinstance = new SomeClass();

var getterAndSetter = prop(someinstance.someProp);
trace(getterAndSetter.get());
trace(getterAndSetter.set(42));

Here is the code: https://gist.github.com/876025

Here is the macro:

/**
 * Object's field/property extraction macro
 * @param  field
 * @return { set : T -> T, get : Void -> T }
 */
@:macro public static function prop(field:Expr)
{
    var pos = Context.currentPos();
    switch (field.expr)
    {
    case EField(_, __):
        // getter
        var getBody:Expr= {expr:EReturn({ expr:field.expr, pos:pos}), pos:pos};
        var getFn = EFunction({ ret:null, name:null, expr:getBody, args:[]});
        // setter
        var fresh = fresh;
        var rval:Expr = {expr:EConst(CIdent(fresh)), pos:pos};
        var assign:ExprDef = EBinop(Binop.OpAssign, field, rval);
        var setBody:Expr = {expr:EReturn({expr:assign, pos:pos}), pos:pos};
        var arg:FunctionArg = {value:null, type:null, opt:false, name:fresh};
        var setFn = EFunction({ret:null, name:null, expr:setBody, args:[arg]});
        // record
        var get = {field:"get", expr: {expr:getFn, pos:pos}};
        var set = {field:"set", expr: {expr:setFn, pos:pos}};
        return {expr:EObjectDecl([get, set]), pos:pos};
    default:
        return Context.error("prop macro: argument must be a field", pos);
    }
}

What are you thinking? It's my first macro and I very appreciate some
comments or review!

Thank You!

Cheers,
  Andrew



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Property extraction macro

Nicolas Cannasse
Le 18/03/2011 14:09, zOOn a écrit :
> Yesterday I made an unpleasant discovery: Haxe cannot Reflect properties!
> How can I tween alpha property in the Matt Tuttle port of FlashPunk? :)
>
> Today I wrote a property extraction macro, it accepts some object's field or
> property and returns anonymous object with getter and setter closures.
[...]
> What are you thinking? It's my first macro and I very appreciate some
> comments or review!

This is a perfect example of macro-driven on-demand code-generation ;)

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Property extraction macro

justin_mills
In reply to this post by zOOn

Quoting zOOn <[hidden email]>:
||Yesterday I made an unpleasant discovery: Haxe cannot Reflect properties!
||How can I tween alpha property in the Matt Tuttle port of FlashPunk? :)

Macros looks interesting, I am probably not following but you can also use Type to help reflect, but not sure if its relevant for what your doing?

for( all in Type.getClassFields( Type.getClass( mc ) )
{

   if ( !Reflect.isFunction( Reflect.field( mc, all ) ) )
   {
       Reflect.setField( mc, all, Reflect.field( recieveObject, all ) );
   }

}


--
haXe - an open source web programming language
http://haxe.org