Search code examples
haxe

Getting the compilation target language in haxe


I know, I can do something like

public static function getTarget():String {
    #if flash
    return "Flash";
    #elseif java
    return "Java";
    //... some more elseif clauses ...
    #end
}

in order to detect the target language in haxe (see http://old.haxe.org/doc/snip/gettarget). However whenever a new target programming language gets added (ok, this is not that frequent) by the community - I would need to add another elseif-clause in order to "support/detect" that language ...

So I was wondering, if there is some kind of predefined macro/function, that returns the target language as string:

trace("This is a " + getTargetLanguage() + " program!");

Solution

  • I don't think such a thing exists.

    To make sure getTarget() doesn't silently break when a new target is added (and you're compiling for it), you could have it throw a compiler error in that case:

    public static function getTarget():String {
        #if flash
        return "Flash";
        #elseif java
        return "Java";
        //... some more elseif clauses ...
        #else
        #error "Missing target name"
        #end
    }