Indexing array in HashLink causing error

I am trying to switch to hashlink, I am having an issue with optional arguments.

public static function Tsp(?names: Array<String>, ?metrics: Array<Array<Float>>=[[]], ?after: Map<String,Int>): Situation {
}

Then I want to iterate the metrics, so hashlink throws a Can't cast hl.types.ArrayObj to hl.types.ArrayBytes_Float

I’ve tried to asign

var cur:Array<Float>
cur=metrics

Haxe compiles fine, but hashlink throws same error.

My mind is thinking of some function that could convert a nullable to a default value, or at least throw exception, so I could try catch.
What is the idiomatic haXe approach?

Thanks

Default.value must be constant, in this case it is null.

	public static function Tsp(?names: Array<String>, ?metrics: Array<Array<Float>>=null, ?after: Map<String,Int>): Dynamic {
		return null;
	}

	var metrics = new Array<Array<Float>>();
	Tsp(null,metrics);

Thanks alot

My mistake.

However, after fixing I’ve narrowed down the issue is something else, but I have no idea what the error means
given these four traces, together with their logs
renamed metrics to m;

54            trace(Type.typeof(m));
55            trace(m);
56            trace(Type.typeof(sit.current));
57           trace(m[sit.current]);

And the logs

nn/NN.hx:54: TClass(hl.types.$ArrayBase)
nn/NN.hx:55: [[1,1,1,478,526,559,559,561,561,544,469,734,734,734,770,1017,1036,1131,1255,1338,679,934],[1,1,1,478,526,559,559,561,561,544,469,734,734,734,770,1017,1036,1131,1255,1338,679,934],[1,1,1,478,526,559,559,561,561,544,469,734,734,734,770,1017,1036,1131,1255,1338,679,934],[489,489,489,1,65,99,99,101,100,139,31,271,271,271,272,648,667,762,1004,969,687,942],[528,528,528,65,1,35,35,47,94,187,71,319,319,319,266,602,621,716,955,923,682,936],[562,562,562,99,35,1,1,55,128,221,104,285,285,285,322,568,587,682,921,890,814,1069],[562,562,562,99,35,1,1,55,128,221,104,285,285,285,322,568,587,682,921,890,814,1069],[564,564,564,101,47,47,47,1,108,222,106,315,315,315,280,598,617,711,951,919,695,950],[571,571,571,101,95,128,128,108,1,161,113,255,255,255,256,472,652,747,986,954,590,845],[551,551,551,136,183,217,217,219,157,1,126,389,389,389,390,744,763,857,1066,1065,590,845],[471,471,471,31,78,112,112,114,113,130,1,335,335,335,285,618,637,732,971,939,700,955],[743,743,743,271,258,292,292,272,256,393,277,1,1,1,37,379,398,492,732,700,530,785],[743,743,743,271,258,292,292,272,256,393,277,1,1,1,37,379,398,492,732,700,530,785],[743,743,743,271,258,292,292,272,256,393,277,1,1,1,37,379,398,492,732,700,530,785],[776,776,776,276,263,325,325,276,260,397,281,34,34,34,1,242,430,525,764,733,534,789],[1031,1031,1031,488,613,579,579,607,472,758,629,261,261,261,238,1,390,485,718,637,638,726],[1039,1039,1039,667,621,587,587,615,651,766,637,396,396,396,432,390,1,165,425,621,925,1180],[1133,1133,1133,762,716,682,682,710,745,861,732,491,491,491,527,485,169,1,389,713,1020,1274],[1263,1263,1263,1007,959,925,925,953,988,1073,975,734,734,734,770,721,414,400,1,791,1263,1517],[1341,1341,1341,969,923,890,890,918,953,1068,939,699,699,699,735,634,617,710,791,1,1227,1385],[692,692,692,695,682,715,715,695,587,592,700,530,530,530,531,641,926,1021,1260,1229,1,256],[947,947,947,950,936,970,970,950,842,847,955,785,785,785,786,733,1181,1276,1515,1428,256,1]]
nn/NN.hx:56: TInt
Uncaught exception: Can't cast hl.types.ArrayObj to hl.types.ArrayBytes_Float

If m is arraybase (actually Array<Array>)
And sit.current is an int
So why does indexing the array crashing hashlink?
And how do i interpret the error words?

Thanks for any help,
I am updating the title to better match this situation

Because you are casting between Array<Float> and Array<Array<Float>> like in following sample?

	var metrics:Array<Array<Float>> = [[1,1,1,478,526,559,559,561,561,544,469,734,734,734,770,1017,1036,1131,1255,1338,679,934],[1,1,1,478,526,559,559,561,561,544,469,734,734,734,770,1017,1036,1131,1255,1338,679,934],[1,1,1,478,526,559,559,561,561,544,469,734,734,734,770,1017,1036,1131,1255,1338,679,934],[489,489,489,1,65,99,99,101,100,139,31,271,271,271,272,648,667,762,1004,969,687,942],[528,528,528,65,1,35,35,47,94,187,71,319,319,319,266,602,621,716,955,923,682,936],[562,562,562,99,35,1,1,55,128,221,104,285,285,285,322,568,587,682,921,890,814,1069],[562,562,562,99,35,1,1,55,128,221,104,285,285,285,322,568,587,682,921,890,814,1069],[564,564,564,101,47,47,47,1,108,222,106,315,315,315,280,598,617,711,951,919,695,950],[571,571,571,101,95,128,128,108,1,161,113,255,255,255,256,472,652,747,986,954,590,845],[551,551,551,136,183,217,217,219,157,1,126,389,389,389,390,744,763,857,1066,1065,590,845],[471,471,471,31,78,112,112,114,113,130,1,335,335,335,285,618,637,732,971,939,700,955],[743,743,743,271,258,292,292,272,256,393,277,1,1,1,37,379,398,492,732,700,530,785],[743,743,743,271,258,292,292,272,256,393,277,1,1,1,37,379,398,492,732,700,530,785],[743,743,743,271,258,292,292,272,256,393,277,1,1,1,37,379,398,492,732,700,530,785],[776,776,776,276,263,325,325,276,260,397,281,34,34,34,1,242,430,525,764,733,534,789],[1031,1031,1031,488,613,579,579,607,472,758,629,261,261,261,238,1,390,485,718,637,638,726],[1039,1039,1039,667,621,587,587,615,651,766,637,396,396,396,432,390,1,165,425,621,925,1180],[1133,1133,1133,762,716,682,682,710,745,861,732,491,491,491,527,485,169,1,389,713,1020,1274],[1263,1263,1263,1007,959,925,925,953,988,1073,975,734,734,734,770,721,414,400,1,791,1263,1517],[1341,1341,1341,969,923,890,890,918,953,1068,939,699,699,699,735,634,617,710,791,1,1227,1385],[692,692,692,695,682,715,715,695,587,592,700,530,530,530,531,641,926,1021,1260,1229,1,256],[947,947,947,950,936,970,970,950,842,847,955,785,785,785,786,733,1181,1276,1515,1428,256,1]];
	Tsp(null,metrics);
	var metrics:Array<Float> = [1,1,1,478,526,559,559,561,561,544,469,734,734,734,770,1017,1036,1131,1255,1338,679,934];
	Tsp(null,cast metrics); // Uncaught exception: Can't cast hl.types.ArrayBytes_Float to hl.types.ArrayObj

The error appears to happen in line 57, I am just tracing, does it do a cast internally?

How can I visualize the hashlink bytecode?

I’ve compiled to a C build to be able to check out, this is what I’ve found

r13 = r11->array;
r8 = ((vdynamic**)(r13 + 1))[r6];
r23 = (hl__types__ArrayBytes_Float)hl_dyn_castp(&r8,&t$_dyn,&t$hl_types_ArrayBytes_Float);

Is there a way to force it to create a float array instead of a dynamic?

It is hard to tell without the source code. From what you said, it seems you’re using dynamic variable holding wrong type. Show minimal sample…

I managed to reproduce error

	var metrics = new Array<Dynamic>();
	metrics[0] = new Array<Cat>();
	Tsp(null,cast metrics); // Can't cast hl.types.ArrayObj to hl.types.ArrayBytes_Float

It is not clear what you are asking. You can create dynamic array acting as float array

	var metrics = new Array<Dynamic>();
	metrics[0] = new Array<Dynamic>();
	metrics[0][0] = 3.14;
	Tsp(null,cast metrics);

You can even use Vector where Array is expected

	var metrics = new Array<Dynamic>();
	metrics[0] = new haxe.ds.Vector<Dynamic>(10);
	metrics[0][0] = 3.14;
	Tsp(null,cast metrics);

But you can’t use object type where basic type like float is expected

I’ve extracted the indexing into a self-contained function, so validate that the error is not happening, but only in my variable as follows

package nn;

class MetrixIndex {
    public static function metrixIndex(ary : Array<Array<Float>>,index:Int):Array<Float> {
        // trace(Type.typeof(ary));
        // trace(Type.typeof(ary[0]));
        // trace(Type.typeof(ary[0][0]));
        return ary[index];
    }
}
//this works
  var ary:Array<Array<Float>>=[[0.0]];
    trace(MetrixIndex.metrixIndex(ary,0));

But my actual case didn’t worked

With trial and error, I figured that Array<Array<Float>> works, but Array<Array<Null<Float>>> is unindexable in haxe, didn’t worked, Once I add a null check to every value added to [[]] everything works.

if (obj==null){
            localMetrics.push(0.0);
          } else {
            localMetrics.push(obj);
          }

My question is how I could identify a Null in the logs?

What is Array<Array> (without type parameter)? Null also requires type parameter, e.g. Array<Array<Null<Float>>>

How is localMetrics defined and related to above array?

You don’t identify a Null (as a type because it doesn’t unify), you simply check if some field have null value like you did in obj==null case.

Sorry, the Rich text editor took away the most inside type, (prob it looks like an HTML tag) I’ve tricked the editor by surrounding it with quotes.