I was hoping the following would work:
void takeOnlyStrings(String* s) {
print(s);
}
{String|Integer*} ab = {"Hi", 1};
takeOnlyStrings(for (item in ab) if (item is String) item);
Or even this:
Boolean isString(Object o) => (o is String);
takeOnlyStrings(ab.filter(isString));
But no luck.
The syntax for narrowing the type of a value is prefix
is Type val
, for example:The syntax
item is String
is just an operator expression that produces aBoolean
value. It doesn't narrow the type of its argument.