Created
August 29, 2015 07:44
-
-
Save RealyUniqueName/65db9e86e46865d5fdc3 to your computer and use it in GitHub Desktop.
Get string representation of function signature
This file contains hidden or 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; | |
import haxe.macro.Type; | |
using haxe.macro.Tools; | |
class Main | |
{ | |
static public function main () : Void | |
{ | |
var fn:Int->Int->Void; | |
var stringType : String = FunctionTypeMacro.resolveType(fn); | |
trace(stringType); | |
} | |
} | |
class FunctionTypeMacro | |
{ | |
macro static public function resolveType (e:Expr) : ExprOf<String> | |
{ | |
var typedExpr = Context.typeExpr(e); | |
switch (typedExpr.t) { | |
case TFun(args,result): | |
var resolved = args.map(function(a) return a.t.toString()).join('->'); | |
resolved += '->' + result.toString(); | |
return macro $v{resolved}; | |
case _: throw 'Unsupported type'; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment