Created
January 20, 2011 22:35
-
-
Save hotpotato/788833 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-main Main | |
-neko main.n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
interface Car | |
{ | |
public function doIt():Void; | |
} | |
class Main | |
{ | |
public static function main():Void | |
{ | |
var c:Car = MyMacro.createMyCarClass().createCar(); | |
c.doIt(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
#if macro | |
import haxe.macro.Context; | |
import haxe.macro.Type; | |
import haxe.macro.Expr; | |
import neko.FileSystem; | |
import neko.io.File; | |
#end | |
class MyMacro | |
{ | |
@:macro public static function createMyCarClass() | |
{ | |
// we create a class file on the fly ;) | |
// this is just a simple way to create it | |
trace("create MyCar class file"); | |
var input = File.write("MyCar.hx", false); | |
input.writeString( | |
' | |
import Main.Car; | |
class MyCar implements Car { | |
public static function createCar () { return new MyCar();} | |
public function new () {} | |
public function doIt():Void trace("Interface implemented on the fly") | |
}'); | |
input.close(); | |
trace("parse and type my created MyCar class"); | |
Context.getType("MyCar"); | |
trace("after get MyCar"); | |
trace("delete MyCar class file"); | |
FileSystem.deleteFile("MyCar.hx"); | |
// return the class, but you can also return a factory or whatever | |
return { expr:EConst(CType("MyCar")), pos:Context.currentPos() }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could do @:macro class MyMacro. Then the whole only exists during compile time...