Sdk: valueOf(key) for Maps like elementAt(index) for Iterables

Created on 2 Jul 2018  路  1Comment  路  Source: dart-lang/sdk

Based on a problem I encountered, I think that it would be a nice addition to the language.

map.valueOf(key)
// which is equal to
map[key]

This enables the null-safe operator:

map?.valueOf(key)

This already exists for iterables, where:

list.elementAt(index)
// is equal to
list[index]
area-language type-enhancement

Most helpful comment

We are not planning on adding a duplicate method. That's bad API design just to make up for a lacking language feature.

We should consider extending null-aware behavior to operators, or at least the index operators. We already allow them for cascades, and we want null-aware operators to extend to the end of the same section like cascades do.
So, maybe at least allow x?.[42] and x?.[42] = 10 like we allow x..[42].toString() and x..[42] = 10.

>All comments

We are not planning on adding a duplicate method. That's bad API design just to make up for a lacking language feature.

We should consider extending null-aware behavior to operators, or at least the index operators. We already allow them for cascades, and we want null-aware operators to extend to the end of the same section like cascades do.
So, maybe at least allow x?.[42] and x?.[42] = 10 like we allow x..[42].toString() and x..[42] = 10.

Was this page helpful?
0 / 5 - 0 ratings