Created
June 9, 2014 08:50
-
-
Save nadako/54ba14280b20b8e9e4df to your computer and use it in GitHub Desktop.
abstract build macro adding compare ops
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
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