Macros: How to request the macro to import a class

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

Macros: How to request the macro to import a class

Cauê W.
Hey guys!
Is there any way to force a macro to include a type, so it can be acessible by the macro via Type.resolveClass() in the execution of the macro?

To be clear, I want to allow the user to implement a function to be called by the macro, that will return e.g. a String with the path to a class. So basically I want to pass a static function path and be able to call it by the macro 


Is there any way to do this?
Thanks!
Cauê

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

Re: Macros: How to request the macro to import a class

Heinz Hölzer-2
Hey Cauê,

yes this is possible with a small and dirty workaround ;)

1) let's say you need to resolve a class named MyClass
2) Inside of your macro build a string that looks like this:

----------------------------------------------------------------------------------------------------------------------------------------------------------------
package;

@:macro class Importer__  {
    public static function build ():haxe.macro.Type {
        // reference your class here:
        MyClass;
        // now MyClass is available in MacroMode
        return haxe.macro.Context.getType("String"); // return some type to satisfy the compiler
    }
}
private typedef Helper__ = haxe.macro.MacroType<Importer__.build()>; // just call the macro
----------------------------------------------------------------------------------------------------------------------------------------------------------------
3) Store this in a class File: f.e. "Helper__.hx"
4) Context.getType("Helper__");
5) Voila, MyClass is available via Type.resolve inside of your macro.
6) You can create Md5 on your params to create a unique class name instead of Helper__

best,
h
 


Am 13.12.2011 16:10, schrieb Cauê Waneck:
Hey guys!
Is there any way to force a macro to include a type, so it can be acessible by the macro via Type.resolveClass() in the execution of the macro?

To be clear, I want to allow the user to implement a function to be called by the macro, that will return e.g. a String with the path to a class. So basically I want to pass a static function path and be able to call it by the macro 


Is there any way to do this?
Thanks!
Cauê




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

Re: Macros: How to request the macro to import a class

Cauê W.
Heinz, thank you for your answer!!
It's indeed a hack but it works! :)

Thank you! : )
Cauê

2011/12/13 Heinz Hölzer <[hidden email]>
Hey Cauê,

yes this is possible with a small and dirty workaround ;)

1) let's say you need to resolve a class named MyClass
2) Inside of your macro build a string that looks like this:

----------------------------------------------------------------------------------------------------------------------------------------------------------------
package;

@:macro class Importer__  {
    public static function build ():haxe.macro.Type {
        // reference your class here:
        MyClass;
        // now MyClass is available in MacroMode
        return haxe.macro.Context.getType("String"); // return some type to satisfy the compiler
    }
}
private typedef Helper__ = haxe.macro.MacroType<Importer__.build()>; // just call the macro
----------------------------------------------------------------------------------------------------------------------------------------------------------------
3) Store this in a class File: f.e. "Helper__.hx"
4) Context.getType("Helper__");
5) Voila, MyClass is available via Type.resolve inside of your macro.
6) You can create Md5 on your params to create a unique class name instead of Helper__

best,
h
 


Am 13.12.2011 16:10, schrieb Cauê Waneck:
Hey guys!
Is there any way to force a macro to include a type, so it can be acessible by the macro via Type.resolveClass() in the execution of the macro?

To be clear, I want to allow the user to implement a function to be called by the macro, that will return e.g. a String with the path to a class. So basically I want to pass a static function path and be able to call it by the macro 


Is there any way to do this?
Thanks!
Cauê




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


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

Re: Macros: How to request the macro to import a class

Heinz Hölzer-2
you're welcome ;)

Am 13.12.2011 18:33, schrieb Cauê Waneck:
Heinz, thank you for your answer!!
It's indeed a hack but it works! :)

Thank you! : )
Cauê

2011/12/13 Heinz Hölzer <[hidden email]>
Hey Cauê,

yes this is possible with a small and dirty workaround ;)

1) let's say you need to resolve a class named MyClass
2) Inside of your macro build a string that looks like this:

----------------------------------------------------------------------------------------------------------------------------------------------------------------
package;

@:macro class Importer__  {
    public static function build ():haxe.macro.Type {
        // reference your class here:
        MyClass;
        // now MyClass is available in MacroMode
        return haxe.macro.Context.getType("String"); // return some type to satisfy the compiler
    }
}
private typedef Helper__ = haxe.macro.MacroType<Importer__.build()>; // just call the macro
----------------------------------------------------------------------------------------------------------------------------------------------------------------
3) Store this in a class File: f.e. "Helper__.hx"
4) Context.getType("Helper__");
5) Voila, MyClass is available via Type.resolve inside of your macro.
6) You can create Md5 on your params to create a unique class name instead of Helper__

best,
h
 


Am 13.12.2011 16:10, schrieb Cauê Waneck:
Hey guys!
Is there any way to force a macro to include a type, so it can be acessible by the macro via Type.resolveClass() in the execution of the macro?

To be clear, I want to allow the user to implement a function to be called by the macro, that will return e.g. a String with the path to a class. So basically I want to pass a static function path and be able to call it by the macro 


Is there any way to do this?
Thanks!
Cauê




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





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