I'd like to draw a small black rectangle and I'd like to do it in strict mode, but can't figure out how. The things I tried:
no viable alternative at input '[[hrx-3,y+2],[hrx,y],[hrx+3,y+2],[hrx-3,y+2]]asArray<Array<Number>>)'
I'd like to draw a small black rectangle and I'd like to do it in strict mode, but can't figure out how. The things I tried:
It looks like there's a bug in the Monkey C grammar where types nested more than one level deep are not recognized by the parser. I would file a bug report for this.
i.e. Array<Number> is valid Monkey C, but not Array<Array<Number>>, although of course constructs similar to the latter appear in the Monkey C documentation.
I managed to work around this with typedef. Of course, each array still has to be explicitly typed, which is super annoying.
e.g.
typedef ArrayOfNumbers as Array<Number>; function testFill(dc as Dc) { var hrx = 5; var y = 2; dc.fillPolygon( [ [hrx - 3, y + 2] as Array<Number>, [hrx, y] as Array<Number>, [hrx + 3, y + 2] as Array<Number>, [hrx - 3, y + 2] as Array<Number> ] as Array<ArrayOfNumbers> ); }
https://forums.garmin.com/developer/connect-iq/i/bug-reports/can-t-define-2-dimensional-array-in-strict-mode.
By the time you posted your answer I changed it to this loop of lines (104 bytes of code):
var dy = gT ? 1 : -1; var ly = y + (gT ? h : -1); for (var i = 1; i <= 5; i++) { dc.drawLine(hrx - i, ly, hrx + i, ly); ly += dy; }
While this is 190 byte:
dc.fillPolygon( [[hrx - 3, y + 2] as Array<Number>, [hrx, y] as Array<Number>, [hrx + 3, y + 2] as Array<Number>, [hrx - 3, y + 2] as Array<Number>] as Array<ArrayOfNumbers>);
Yeah arrays are expensive. IIRC there's about 12 bytes of overhead per array. (And that's only for the object memory, to say nothing of the code for initializing the arrays.)
Arrays are complex objects, and a multiply decisional array (an array of arrays is an array of complex objects)
So things take more memory than an array of simple objects (Booleans, Numbers, Floats)
A simple object is where the value will fit in 32 bits. For a complex objects, that 32 bits us used as a pointer to the data.
This is also why an array of floats take less memory than an array of doubles. Doubles aren't simple objects.