can I get one var's type during compile-time in macro?

58 views
Skip to first unread message

davy zhang

unread,
Feb 19, 2014, 2:22:46 AM2/19/14
to haxe...@googlegroups.com
I have some code to port from as3. 
somebody just get so addicted to to using code like this:
if (a){
...
}
while (a){
...
}


where a could be anything, its a headache to port them, I have to go through all the code to read and determine which type actually is, 
since it's impossible to fixed it using regex
the first thing I tried is using the RTTI.

but after that I thought since haxe using type inference, I might do this in compile time, but after I scratch my head for a morning I can't get this right.

so there's no way to get type in macro in haxe?

Jason O'Neil

unread,
Feb 19, 2014, 2:44:23 AM2/19/14
to haxe...@googlegroups.com
You can use a special method:

$type(a)

To get the compiler to print the type it has inferred.  It often works, but sometimes there is not enough type information for it to know.

In terms of getting types by writing macros, there is Context.typeof(), but I don't think it would help in this specific situation as your code has to already be complete to a certain degree before a macro will be able to execute.

Jason


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.

davy zhang

unread,
Feb 19, 2014, 4:43:07 AM2/19/14
to haxe...@googlegroups.com, he...@jasono.co
Thanks so much! you made my day:D

with the your suggustion I finally made it possible using Context.typeof();

All I need to is wrap the if (var) with the wrapper function using regex to replace the original code.

the macro I manage to write is below: a little mess with debugs :P

package sandbox ;
import haxe.macro.Context;
import haxe.macro.Expr;

/**
 * ...
 * @author dawn
 */
class TestMacro
{

    macro static function BoolType( v:Expr ) {
        //var str : ComplexType = macro : String;
        //var arr : ComplexType = macro : Array < Array < $str >> ;
//var bl :ComplexType = macro : Bool;
        //return macro var ss:$bl = ($vname == "");

//return Context.makeExpr(($v == ""), Context.currentPos());
//trace("type ", Type.typeof(macro $v));

trace("name is ", Context.typeof(v));
switch (Context.typeof(v)) 
{
case TInst(t, params):
var tn = t.toString();
if (tn == "String") {
return macro ($v != "");
//return Context.makeExpr(($v != ""), Context.currentPos());
}else {
return macro ($v != null);
//return Context.makeExpr(($v != null), Context.currentPos());
}
case TAbstract(t, params):
var tn = t.toString();
if ((tn == "Int") || (tn == "Float")) {
return macro ($v != 0);
//return Context.makeExpr(($v != 0), Context.currentPos());
}else if (tn == "Bool") {
return macro $v;
//return Context.makeExpr(($v), Context.currentPos());
}
case TDynamic(t):
return macro ($v != null);
default:

}
trace("at last");
return Context.makeExpr("new type found", Context.currentPos());
    }
    
    static function main() {
new TestMacro();
    }
    
public function new() {
var s:Dynamic = null;
trace(BoolType(s));// == false);
var s = 0;            
trace(BoolType(s));// == false);
var s = 1;            
trace(BoolType(s));// == true);
var s = "";           
trace(BoolType(s));// == false);
var s = "222";        
trace(BoolType(s));// == true);
//TestMacro.hx:60: false
//TestMacro.hx:62: false
//TestMacro.hx:64: true
//TestMacro.hx:66: false
//TestMacro.hx:68: true
}

}
Reply all
Reply to author
Forward
0 new messages