Skip to content

Instantly share code, notes, and snippets.

@SimonRichardson
Created April 30, 2013 08:46
Show Gist options
  • Select an option

  • Save SimonRichardson/5487453 to your computer and use it in GitHub Desktop.

Select an option

Save SimonRichardson/5487453 to your computer and use it in GitHub Desktop.
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
}
@SimonRichardson
Copy link
Copy Markdown
Author

package ;

class Test {
static function main(){
var k = new Kill(1);
trace(k.bill);
}
}

class Kill implements CaseClass {
var bill : Int;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment