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
}
}