3 ms·
> So, rather than calling func(some(3)), you'd just call func(3) and the compiler would automatically wrap it up. You can do implicit conversions and that help
by munificent 6y ago
> So, rather than calling func(some(3)), you'd just call func(3) and the compiler would automatically wrap it up.
You can do implicit conversions and that helps somewhat, but it's not quite the same as actual subtyping. In many cases, there's no natural or efficient way to insert that conversion so you still run into restrictions. For example, in a language with nullable types you can write:
int sumPresentValues(Iterable<int?> values) {
var result = 0;
for (var value in values) if (value != null) result += value;
return result;
}
main() {
Iterable<int> values = [1, 2, 3, 4];
print(sumPresentValues(values)); // <--
}
The marked line is passing an `Iterable<int>` to a function expecting an `Iterable<int?>`. That works because `int` is an actual subtype of `int?` with no conversion required. With a boxing step, you'd need to somehow wrap the entire collection in one that does the conversion.