Skip to content

Instantly share code, notes, and snippets.

@nadako
Created June 9, 2014 08:50
Show Gist options
  • Save nadako/54ba14280b20b8e9e4df to your computer and use it in GitHub Desktop.
Save nadako/54ba14280b20b8e9e4df to your computer and use it in GitHub Desktop.
abstract build macro adding compare ops
import haxe.macro.Context;
import haxe.macro.Expr;
class AbstractCompare
{
public static function build():Array<Field>
{
var ab = switch (Context.getLocalType())
{
case TInst(_.get() => {kind: KAbstractImpl(a)}, _):
a.get();
default:
throw new Error("AbstractCompare can only be used on abstracts", Context.currentPos());
}
var ct = TPath({pack: ab.pack, name: ab.name});
var ctReal = haxe.macro.TypeTools.toComplexType(ab.type);
var cls = macro class A {
@:impl @:op(A < B) function lt(self:$ctReal, other:$ct):Bool;
@:impl @:op(A <= B) function lte(self:$ctReal, other:$ct):Bool;
@:impl @:op(A > B) function gt(self:$ctReal, other:$ct):Bool;
@:impl @:op(A >= B) function gte(self:$ctReal, other:$ct):Bool;
}
var fields = Context.getBuildFields();
fields = fields.concat(cls.fields);
return fields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment