Can you check class existence at compile time


#1

I was wondering …
can you check class existence at compile time ?

For exemple I have a class MyClass.

Can I do something like #if MyClass trace (“MyClass exists!”); #else (“MyClass doesn’t exist!”); #end


#2

Not that I know of, but I would be interested in this if anyone has a solution


#3

If singmajesty doesn’t have a solution, I’m not sure anybody has :wink: But i feel this is a nice feature that could be missing.


#4

In a macro, you can use Context.getModule() or Context.getType().

import haxe.macro.Context;
import haxe.macro.Expr;

class CompileTime {
    public static macro function typeExists(fullyQualifiedName:String):Expr {
        try {
            if(Context.getType(fullyQualifiedName) != null) {
                return macro true;
            }
        } catch(error:String) {}
        return macro false;
    }
}
if(CompileTime.typeExists("MyClass")) {
    trace("MyClass exists!");
} else {
    trace("MyClass doesn't exist!");
}

#5

Would that force the type to be included, if no code already referenced it?


#6

That’s a good question, and I have no clue. If I had to guess, I’d guess not.