我目前正在将我的C#代码移植到Haxe,但我很难辨别如何使用C#参数修饰符,例如Out或来自Haxe的参考
例如,Out参数在许多C#Generic集合中的TryGetValue等函数中使用。在下面使用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作为参数修饰符的正确方法?谢谢。
答案 0 :(得分:0)
如文档中所述,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() {}
}