Skip to content

Instantly share code, notes, and snippets.

@RealyUniqueName
Created August 29, 2015 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealyUniqueName/65db9e86e46865d5fdc3 to your computer and use it in GitHub Desktop.
Save RealyUniqueName/65db9e86e46865d5fdc3 to your computer and use it in GitHub Desktop.
Get string representation of function signature
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