Macros: Context.defineType cannot define class extending from existing class

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Macros: Context.defineType cannot define class extending from existing class

David
Hi all,

I've been playing with the cool new 2.08 features :-D

Wondering if I've done something wrong in the code below (at the
bottom). The key part is the macro is trying to define a class
extending from BaseClass. The compiler says "Class not found :
BaseClass", even though it's right there.

Also, this defining of types is very cool. I'm sure many would be
familiar with Tweening libraries. Like if you have a function a bit
like this:
TweenTo (some_variable, duration, end_value), the annoying part was
always how to actually implement "some_variable".

For my own tweener I had used reflection so calling the function
actually looked like this:
TweenTo (some_object, "some_var", duration, end_value)

I never really liked it. But now TweenTo can be a macro which
generates a new type of setter class on the fly, and hands an instance
of this new setter to the tweening code. And then you can simply call
it with any lvalue expression directly:
TweenTo (some_object.some_var, duration, end_value)

It's also nearly 10 times faster than using reflection

I love it!

David


import haxe.macro.Expr;
import haxe.macro.Context;

class BaseClass {
       
}

class Main {
        @:macro public static function MakeType () : Expr {
                var pos = Context.currentPos ();
                var newclass = {
                        pack : [],
                        name : "NewClass",
                        pos : pos,
                        meta : [],
                        params : [],
                        isExtern : false,
                        kind : TDClass ({pack : [], name : "BaseClass", params : [], sub :
null}, null, false),
                        fields : []
                };
                Context.defineType (newclass);
                return {expr : EBlock([]), pos : pos};
        }
       
        static function main() {
                MakeType ();
        }
}

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