Getting the compilation target language in haxe
Asked Answered
I

1

5

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!");
Inconceivable answered 11/3, 2018 at 13:53 Comment(0)
B
7

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
}
Barometer answered 11/3, 2018 at 14:16 Comment(1)
Thanks for coming up with the "#error" feature. This compiler feature can indeed be very helpful in such cases ...Inconceivable

© 2022 - 2024 — McMap. All rights reserved.