Created
April 30, 2013 08:46
-
-
Save SimonRichardson/5487453 to your computer and use it in GitHub Desktop.
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
| package ; | |
| import haxe.macro.Expr; | |
| import haxe.macro.Context; | |
| @:remove @:autoBuild(CaseClassImpl.build()) | |
| extern interface CaseClass {} | |
| class CaseClassImpl { | |
| #if macro | |
| public static function build() { | |
| var fields = Context.getBuildFields(); | |
| var args = []; | |
| var states = []; | |
| for (i in 0...fields.length) { | |
| var f = fields[i]; | |
| switch (f.kind) { | |
| case FVar(t, e): | |
| var name = f.name; | |
| f = { | |
| name: name, | |
| access: [APublic], | |
| pos: Context.currentPos(), | |
| kind: FProp( | |
| 'get_${f.name}', | |
| 'never', | |
| t, | |
| null | |
| ) | |
| }; | |
| fields[i] = f; | |
| fields.push({ | |
| name: '_${f.name}', | |
| access: [APrivate], | |
| pos: Context.currentPos(), | |
| kind: FVar(t, e) | |
| }); | |
| fields.push({ | |
| name: 'get_${f.name}', | |
| access: [APublic], | |
| pos: Context.currentPos(), | |
| kind: FFun({ | |
| args: [], | |
| expr: macro return $i{'_${f.name}'}, | |
| params: [], | |
| ret: t | |
| }) | |
| }); | |
| args.push({name : name, type : t, opt : false, value : null}); | |
| states.push(macro $p{['_${f.name}']} = $i{name}); | |
| case _: | |
| } | |
| } | |
| fields.push({ | |
| name: "new", | |
| access: [APublic], | |
| pos: Context.currentPos(), | |
| kind: FFun({ | |
| args: args, | |
| expr: macro $b{states}, | |
| params: [], | |
| ret: null | |
| }) | |
| }); | |
| return fields; | |
| } | |
| #end | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
package ;
class Test {
static function main(){
var k = new Kill(1);
trace(k.bill);
}
}
class Kill implements CaseClass {
var bill : Int;
}