[英]How can you use C# parameter modifiers from Haxe?
I'm currently porting my C# code to Haxe, but am having difficulty discerning how to use C# parameter modifiers such as Out or Ref from Haxe 我目前正在将C#代码移植到Haxe,但是很难辨别如何使用Haxe的C#参数修饰符,例如Out或Ref。
For example, the Out parameter is used in functions like TryGetValue in many of the C# Generic collections. 例如,在许多C#Generic集合中的TryGetValue之类的函数中都使用了Out参数。 In the example below using Haxe, it passes Haxe compilation but complains when compiling with the Mono compiler that an Out modifier is required, which is what I had thought it was doing.
在下面使用Haxe的示例中,它通过Haxe编译,但是在使用Mono编译器进行编译时抱怨需要使用Out修饰符,这就是我认为的做法。
Example Code 范例程式码
// 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
}
Links to the Parameter Typedefs in Haxe for Parameter Modifiers in C# 链接到Haxe中用于C#中参数修改器的参数Typedef
Is there anyone who can show me the proper way to use the Haxe typedefs for parameter modifiers in Haxe? 有谁可以向我展示在Haxe中使用Haxe typedef作为参数修饰符的正确方法吗? Thank you.
谢谢。
As noted in the doc, the Out
type is meant to be used in function parameters only. 如文档中所述,
Out
类型只能在函数参数中使用。 So basically you only need to type your variable normally. 因此,基本上,您只需要正常键入变量即可。
The following code works for me: 以下代码对我有用:
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.