简体   繁体   中英

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

For example, the Out parameter is used in functions like TryGetValue in many of the C# Generic collections. 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.

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#

Is there anyone who can show me the proper way to use the Haxe typedefs for parameter modifiers in Haxe? Thank you.

As noted in the doc, the Out type is meant to be used in function parameters only. 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() {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM