3 ms·
> Finally, consider this example of chaining the subscript: > someJson?[recipes]?[2]?[ingredients]?[pepper] > To our eyes, that doesn’t look very good. It sca
by d--b 7y ago
> Finally, consider this example of chaining the subscript:
> someJson?[recipes]?[2]?[ingredients]?[pepper]
> To our eyes, that doesn’t look very good. It scans less like a method chain and more like some combination of infix operators — a little like ??. Compare it to this code:
> someJson?.[recipes]?.[2]?.[ingredients]?.[pepper]
Yep. So in the end ?[] does not look better, and the dot avoids the ambiguity with ? [] :
There you go. Dot. You could also allow `a.[x]` as a valid alternative to a[x]
- yoz-y 7y agoWhy exactly is it a problem when the intent here is to actually chain infix operators.
- ivanbakel 7y agoBecause `?` is not an infix operator between two things of the same (or reasonably similar) type, which is what programmers expect from infix operator chains.
- yoz-y 7y agoI see. Coming from swift the ?. syntax feels weird to me because it implies there is some property accessed or method called when there is not. Ambiguity with ternary operator is unfortunate but this one is lifted (in Swift) by harder rules on whitespace. A postfix operator must not have space between the variable and the ?: operator must have space around.
- 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.
- deleted 7y ago[deleted]