Created
February 22, 2013 07:48
-
-
Save Simn/5011604 to your computer and use it in GitHub Desktop.
Haxe abstract builder example
This file contains hidden or 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
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
import haxe.macro.Type; | |
class AbstractBuilder { | |
macro static public function build():Array<Field> { | |
var fields = Context.getBuildFields(); | |
var cCur = Context.getLocalClass().get(); | |
var fieldMap = [for (f in fields) f.name => true]; | |
function loop(c:ClassType) { | |
for (f in c.fields.get()) { | |
if (f.meta.has(":abstract")) { | |
if (!fieldMap.exists(f.name)) { | |
Context.error("Missing implementation for abstract field " +f.name, cCur.pos); | |
} | |
} else { | |
fieldMap.set(f.name,true); | |
} | |
} | |
if (c.superClass != null) | |
loop(c.superClass.t.get()); | |
} | |
if (cCur.superClass != null) | |
loop(cCur.superClass.t.get()); | |
return null; | |
} | |
} |
This file contains hidden or 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
@:autoBuild(AbstractBuilder.build()) | |
class MyAbstractBaseClass { | |
private function new() { } | |
@:abstract public function intToString(i:Int):String { | |
return throw "not implemented"; | |
} | |
public function common() { | |
return "bar"; | |
} | |
} | |
class ChildClass extends MyAbstractBaseClass { | |
public function new() { | |
super(); | |
} | |
//override public function intToString(i:Int):String { | |
//return "foo"; | |
//} | |
} | |
class Main { | |
static function main() { | |
var x = new ChildClass(); | |
trace(x.intToString(12)); | |
trace(x.common()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment