Can you check class existence at compile time

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

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

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.

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!");
}
1 Like

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

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