2

Haxeがコンパイルされた言語に応じて関数の動作を変更できるように、Haxeでターゲット言語を検出したいと思います。

Haxe に似た擬似コードの例:

class Test() {
static function printStuff(toPrint) {
    if (the target language is Java) {
        System.out.println(toPrint);
    } else if (the target language is C++) {
        cout << toPrint;
    } else if (the target language is JavaScript) {
        alert(toPrint);
    }

}
}

現在Haxeでこれを達成することは可能ですか?

4

3 に答える 3

10

これを実現するために、 Haxe Magicとともに条件付きコンパイルを使用できます。例えば:

#if java
    untyped __java__("java.lang.System.out.println(toPrint);");
#elseif js
    untyped __js__("alert(toPrint);");
#elseif ...
    ...
#end
于 2012-07-20T03:54:48.417 に答える
5

Or even just use trace.

class Test()
{
   static function main()
   {
       #if java
           var language = 'java';
        #elseif js
            var language = 'js';
        #elseif cs
            var language = 'csharp';
        #elseif php
            var language = 'PHP'
        #elseif (flash||flash8)
            var language = 'flash';
        #elseif cpp
            var language = 'c++';
        #elseif neko
            var language = 'neko';
        #elseif tamarin
            var language = 'tamarin';
        #end
        trace( language );
    }   
}

But it should be noted that the hxml for compiling this would need to have each target specified in theory it would be something generic like...

-java java
-main Test
--next
-js test.js
-main Test
--next
-cs cs
-main Test
--next
-php www
-main Test
--next
-swf test8.swf
-swf-version 8
-main Test
--next
-swf test.swf
-swf-version 9
-main Test
--next
-neko neko
--main Test

But in practice you will probably want to add other compiler flags and even use -cmd to actually run examples.

Getting started with a range of targets...

'http://haxe.org/doc/start/

conditional compilation

'http://haxe.org/ref/conditionals

compiler flags and options, although I may have missed a link.

'http://haxe.org/manual/tips_and_tricks 'http://haxe.org/doc/compiler

Then target magic

'http://haxe.org/doc/advanced/magic

For each target you can use generic haxe api along with target specific libraries named after the target

'http://haxe.org/api

My first reply on Stackoverflow so sorry if it's a bit verbose :) also the reason I can't post proper links (limited to two).

于 2012-07-25T10:05:43.687 に答える
2

申し訳ありませんが、なぜタイプされていない方法を示したのですか?

完全に入力され、オートコンプリートされたコードを持つことができます

class Test() {
    static function printStuff(toPrint) {
        #if java
            java.lang.System.out.println(toPrint);
        #elseif js
            js.Lib.alert(toPrint);
        #elseif cpp
            cpp.Lib.print(toPrint);
        #end
    }
}
于 2012-07-21T07:33:57.980 に答える