Skip to content

Instantly share code, notes, and snippets.

@nadako
Created March 18, 2020 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadako/b989a8c6a4c69715e5b2cca41c0d0e19 to your computer and use it in GitHub Desktop.
Save nadako/b989a8c6a4c69715e5b2cca41c0d0e19 to your computer and use it in GitHub Desktop.
declaration-site unification check
class Point implements UnifyCheck<{x:Int}> {
public var x:Int;
}
class Main {
static function main() {
}
}
@:remove
@:autoBuild(UnifyCheckMacro.build())
interface UnifyCheck<T> {}
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using haxe.macro.Tools;
class UnifyCheckMacro {
static function build() {
var fields = Context.getBuildFields();
var localClass = Context.getLocalClass().get();
for (iface in localClass.interfaces) {
switch iface {
case {t: _.toString() => "UnifyCheck", params: [type]}:
// TODO: handle type params
var thisCT = Context.getLocalType().toComplexType();
var unifyCT = type.toComplexType();
fields.push((macro class {
extern inline function __unifyCheck__() @:pos(Context.currentPos()) ((null : $thisCT) : $unifyCT);
}).fields[0]);
break;
case _:
}
}
return fields;
}
}
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment