How can I keep the Generics type in the C# code output from Haxe?

Viewed 92

I'd like to use an Array with type in C#.

I tried building the following code in Haxe 4.0.5, but hoges is an Array<object> in C#. (I wanted Array<Hoge>)

class ArrayTest
{
    public var hoges: Array<Hoge>;
}

class Hoge
{
    public var x: Int;
    public var y: Int;
    public var z: Int;
}

I found the following post on github and understand that this behavior is a spec to make the code faster. https://github.com/HaxeFoundation/haxe/issues/5434#issuecomment-230581990.

However, I'm hoping it comes with a type because I want to use this code as an interface. Are there any workarounds?

2 Answers

If it is primarily for purposes of interfacing with external code, using a C#-specific collection can be more fitting:

import cs.system.collections.generic.List_1;

class Main {
    public static var hoges:List_1<Hoge> = new List_1();
    static function main() {
        hoges.Add(new Hoge());
        trace(hoges[0]);
    }
}
class Hoge {
    public var x: Int;
    public var y: Int;
    public var z: Int;
    public function new() {}
}

which produces

public static global::System.Collections.Generic.List<global::Hoge> hoges;

as you would expect.

Abstracts can be used to switch implementations depending on the target platform.

You can use NativeArray

typedef Hoges = cs.NativeArray<Hoge>;
class ArrayTest { public var hoges: Hoges; }

generating

public global::Hoge[] hoges;
Related