chuck
chuck copied to clipboard
Associative arrays are not multidimensional
The doc does not make it clear if associative arrays can be multidimensional, it appears not
$ cat multi-assoc.ck
class Foo {
string name;
}
Foo assoc1d[0];
Foo assoc2d[0][0];
Foo foo1;
"1" => foo1.name;
Foo foo2;
"2" => foo2.name;
foo1 @=> assoc1d["one"];
foo2 @=> assoc1d["two"];
<<<"assoc1d", assoc1d["one"].name, assoc1d["two"].name>>>;
foo1 @=> assoc2d["foo"]["one"];
<<<"assoc2d", assoc2d["foo"]["two"].name>>>;
$ chuck multi-assoc.ck
assoc1d 1 2
[chuck](VM): NullPointerException: (array access) on line[19] in shred[id=1:multi-assoc.ck]
[chuck](VM): (array dimension where exception occurred: 1)
Note in the error message (array access)
shows ChucK is trying to use the wrong dimension.
My best guess on how you might do this would be this, but it gives a different kind of null pointer exception (at least on webchuck, where I tested it):
float foo[][];
float subfoo1[];
float subfoo2[];
subfoo1 @=> foo["one"];
subfoo2 @=> foo["two"];
5 => foo["one"]["a"];
6 => foo["two"]["b"];
<<< foo["one"]["a"], foo["two"]["b"] >>>;