Created
August 30, 2015 23:03
-
-
Save nadako/87bd48d98e70805ef8cc to your computer and use it in GitHub Desktop.
Code example for https://groups.google.com/forum/#!topic/haxelang/yNIkCx7w0t4
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
class Main { | |
static function main() { | |
var s = new Signature<Int->Void>(); | |
} | |
} |
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
// Generated by Haxe | |
(function () { "use strict"; | |
var Main = function() { }; | |
Main.main = function() { | |
var s = "Int -> Void"; | |
}; | |
Main.main(); | |
})(); |
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
#if macro | |
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
import haxe.macro.Type; | |
using haxe.macro.Tools; | |
#end | |
#if !macro | |
@:genericBuild(Signature.build()) | |
#end | |
@:dce | |
class Signature<T> { | |
#if macro | |
static function build():ComplexType { | |
var typeString = switch (Context.getLocalType()) { | |
case TInst(_, [type]): type.toString(); | |
default: | |
throw false; | |
} | |
var pos = Context.currentPos(); | |
var name = "Signature$" + typeString; | |
var ctor = { | |
pos: pos, | |
name: "new", | |
kind: FFun({ | |
args: [], | |
ret: null, | |
expr: macro this = $v{typeString} | |
}), | |
access: [APublic, AInline], | |
}; | |
Context.defineType({ | |
pos: pos, | |
pack: [], | |
name: name, | |
kind: TDAbstract(macro : String, null, [macro : String]), | |
fields: [ctor], | |
meta: [{name: ":dce", pos: pos}] | |
}); | |
return TPath({pack: [], name: name}); | |
} | |
#end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment