我目前正在将我的 C# 代码移植到 Haxe,但我很难辨别如何使用来自 Haxe 的 C# 参数修饰符,例如 Out 或 Ref
例如,Out 参数用于许多 C# 通用集合中的 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
}
C# 参数修饰符的 Haxe 参数类型定义链接
有没有人可以告诉我在 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() {}
}
关于c# - 如何使用 Haxe 的 C# 参数修饰符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29897319/