I have a typedef that can be either a tuple of length 1 or a tuple of length 2:
typedef ViewTuple as [WatchUi.Views] or [WatchUi.Views, WatchUi.InputDelegates];
(i.e. the return value of AppBase.getInitialView())
In my code, I have a function that takes a ViewTuple as an argument and tries to figure out which kind of tuple it is (length 1 or length 2) so it can treat it appropriately.
if (viewTuple.size() == 2) {
WatchUi.switchToView(viewTuple[0], viewTuple[1], transition);
} else {
WatchUi.switchToView(viewTuple[0], null, transition);
}
Note that in the first case, I have explictly checked the length of the tuple array and can be sure. But I get the following error:
Array index is out of bounds for type '$.Toybox.Lang.Array[$.Toybox.WatchUi.Confirmation or $.Toybox.WatchUi.Menu or $.Toybox.WatchUi.NumberPicker or $.Toybox.WatchUi.ProgressBar or $.Toybox.WatchUi.TextPicker or $.Toybox.WatchUi.View or $.Toybox.WatchUi.ViewLoop]'.
I can resolve this by adding an additional typecast:
if (viewTuple.size() == 2) {
viewTuple = viewTuple as [WatchUi.Views, WatchUi.InputDelegates];
WatchUi.switchToView(viewTuple[0], viewTuple[1], transition);
} else {
WatchUi.switchToView(viewTuple[0], null, transition);
}
But this looks really kludgy and is obviously unnecessary. IMO, the type checker should be smart enough to figure out which case of the PolyType we are dealing with here and that indexing the array at index 1 will not cause an error.