Mutating `[T]?` with a default value, or: returning inout, or: extending Any

Hmm, maybe I am out of touch, but I haven't seen much Swift code that seemingly avoided or was so unaware of Optional.map as to write unnecessarily verbose pattern-matching code (although I would expect there is less familiarity with flatMap). And since Optional isn't actually a Collection, I find it hard to believe that people would find subscript notation to be at all natural on an Optional.

It's funny to imagine how it could be used if it were actually a collection: does unwrapping require a runtime-checked opt[opt.startIndex] operation? :grimacing: Does .first simply return self? :exploding_head: Although it has been discussed:

It might even be more confusing for actual collection types, where collection[ something ] almost always indicates we're extracting an element of the collection, but here we're just providing a different, mutable, view of the collection.

1 Like