Using haxe nested arrays from C# side

75 views
Skip to first unread message

azrafe7

unread,
Apr 11, 2015, 11:17:34 AM4/11/15
to haxe...@googlegroups.com
I'd appreciate some hints with working with a haxe generated DLL from C#.

The problem I'm facing (as far as I understand it!), seems related to hxcs converting nested arrays into `Array<object>`, thus acting as untyped on the C# side.

Here's a simplified example of what I'm trying to do:

Tools.hx (converted to a net-ver=40 dll):
```haxe
typedef Sequences = Array<Array<Float>>;

class Tools {
static public function getRandSequences():Sequences {
var res:Sequences = [];
for (i in 0...3) {
res.push([for (j in 0...5) j]);
}
return res;
}
static public function flattenSequences(sequences:Sequences):String {
var stringBuf = new StringBuf();
for (seq in sequences) {
for (n in seq) {
stringBuf.add(Std.string(n) + " ");
}
stringBuf.add("\n");
}
return stringBuf.toString();
}

static public function addUpSequences(sequences:Sequences):Float {
var sum = 0.0;
for (seq in sequences) {
for (n in seq) {
sum += n;
}
}
return sum;
}
}
```

Program.cs:
```cs
...
namespace ConsoleApp
{
    using Sequences = Array<object>;            // Actually Array<Array<Double>>, but works!
    //using Sequences = Array<Array<Double>>;   // Error: Cannot implicitly convert type 'Array<object>' to 'Array<Array<double>>'

    class Program
    {
        static void Main(string[] args)
        {
            Sequences sequences = Tools.getRandSequences();
            
            String str = Tools.flattenSequences(sequences);
            System.Diagnostics.Debug.WriteLine(str);
            
            Double sum = Tools.addUpSequences(sequences);
            System.Diagnostics.Debug.WriteLine(sum);

            //Double firstNum = (Array<Double>)(sequences[0])[0];  // how can I access this, or - better - emulate the typedef from haxe
        }
    }
}
```


azrafe7

unread,
Apr 11, 2015, 11:23:19 AM4/11/15
to haxe...@googlegroups.com
Sorry for the formatting (used to github markdown: how do I do it here?!), and couldn't find a preview button to doublecheck it! ;D

Gama11

unread,
Apr 11, 2015, 5:58:27 PM4/11/15
to haxe...@googlegroups.com

azrafe7

unread,
Apr 12, 2015, 10:03:36 AM4/12/15
to haxe...@googlegroups.com
Thanks Gama!

I think I've found a solution to my problem. This seems to work (but any comment to know if it's the correct way to do it is welcome):

    ...
    using Sequences = Array<Array<double>>;

    class Program
    {
        static void Main(string[] args)
        {
                     var sequences = Tools.getRandSequences();     // Array<object>
            System.Diagnostics.Debug.WriteLine(sequences.GetType());
            Sequences typedSequences = (Sequences)sequences.Array_cast<Array<double>>();  // Array<Array<double>>
            
            String str = Tools.flattenSequences(sequences);
            System.Diagnostics.Debug.WriteLine(str);
            
            Double sum = Tools.addUpSequences(sequences);
            System.Diagnostics.Debug.WriteLine(sum);

            double num = typedSequences[0][0];
            System.Diagnostics.Debug.WriteLine(num);
        }
    }


Reply all
Reply to author
Forward
0 new messages