簡體   English   中英

如何使用Haxe的C#參數修飾符?

[英]How can you use C# parameter modifiers from Haxe?

我目前正在將C#代碼移植到Haxe,但是很難辨別如何使用Haxe的C#參數修飾符,例如Out或Ref。

例如,在許多C#Generic集合中的TryGetValue之類的函數中都使用了Out參數。 在下面使用Haxe的示例中,它通過Haxe編譯,但是在使用Mono編譯器進行編譯時抱怨需要使用Out修飾符,這就是我認為的做法。

范例程式碼

// In C#, looks like this..`.
KeyValuePair<T,V>? element = null;
if (!dictionary.TryGetValue(key, out element))
{
     // Do foo
}

// In Haxe?
var element:Out<KeyValuePair<T,V>> = null;
if (!dictionary.TryGetValue(key, element))
{
     // Do foo
}

鏈接到Haxe中用於C#中參數修改器的參數Typedef

有誰可以向我展示在Haxe中使用Haxe typedef作為參數修飾符的正確方法嗎? 謝謝。

如文檔中所述, Out類型只能在函數參數中使用。 因此,基本上,您只需要正常鍵入變量即可。

以下代碼對我有用:

import cs.system.collections.generic.Dictionary_2;

class Main {
    static function main() {
        var dictionary = new Dictionary_2<String, Foo>();
        dictionary.Add('haxe', new Foo());

        // type inference works
        var implicit = null;
        dictionary.TryGetValue('haxe', implicit);
        trace(implicit);

        // this also works
        var explicit:Foo = null;
        dictionary.TryGetValue('haxe', explicit);
        trace(explicit);
    }
}

class Foo {
    public function new() {}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM