I have the following simplified code:
[Integer, String]? x = [1, ""];
I want to get either element of the tuple. Problem being, cannot do it without losing the type information.
Examples:
String? s = x?[1]; // incorrect syntax
String? s = x?.get(1); // cannot assign String|Integer?
String? s = x?.last // cannot assign String|Integer?
I got it working like this:
String? s = if (exists x) then x[1] else null; // OK!
But it feels unsatisfying... in particular, I believe the first example above should have worked...
value x = [2, "hi"];
String s = x[1]; // OK
[Integer, String] y = [3, "ho"];
String? s = x?[1]; // invalid syntax :(
Why doesn't the syntax allow that?
PS: the real use case is this: I need to get the longest list in a list of lists, so I sort the lists by size and get the first list:
{{Integer*}*} list = { {1, 2}, {4, 3, 2} }; // example
value longest = [ for (c in list) [c.size, c] ]
.sort((first, sec) => first[0] <=> sec[0])
.first?.last else {}; // just need the longest list
// Not good, type of longest is Integer|{Integer*}
// because of the problem I explained above
PS2: getting the sizes of the Iterables may be expensive but that does not matter in this example