3 ms·
> Coming from swift the ?. syntax feels weird to me because it implies there is some property accessed or method called when there is not. There is. "[]" is a
by munificent 7y ago
> Coming from swift the ?. syntax feels weird to me because it implies there is some property accessed or method called when there is not.
There is. "[]" is a method in Dart. In Dart, almost all operators are syntactic sugar for corresponding method calls on the left-hand operand. In Dart, lists and maps are built into the core library, but the "[]" syntax is not special to those types. Any user-defined class is free to implement the subscript operator too.
In practice, this is useful because it means user-defined classes can implement the List and Map interfaces. This gives user collections the same ease of use as built-in ones.