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

**URL:** <https://forums.swift.org/t/mutating-t-with-a-default-value-or-returning-inout-or-extending-any/40593>\
**Category:** Discussion\
**Tags:** subscripts, coroutines\
**Created:** [September 24, 2020, 8:51pm UTC](https://forums.swift.org/t/mutating-t-with-a-default-value-or-returning-inout-or-extending-any/40593 "2020-09-24T20:51:38Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![jtbandes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jtbandes/32/463_2.png) [@jtbandes](https://forums.swift.org/u/jtbandes)\
**Post date:** [September 24, 2020, 9:57pm UTC](https://forums.swift.org/t/mutating-t-with-a-default-value-or-returning-inout-or-extending-any/40593/8 "2020-09-24T21:57:07Z")

</div>

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? 😬 Does `.first` simply return `self`? 🤯 Although it has been discussed:

> [@Add an ifPresent function to Optional](https://forums.swift.org/t/add-an-ifpresent-function-to-optional/1759/5):
>
> +1. I’d go so far as to suggest that maybe Optional should conform to CollectionProtocol, since you can pretty easily think of an Optional as a collection that can’t have more than one element. - Dave Sweeris 
> 
> > **···**
> >
> > On Mar 10, 2016, at 7:32 PM, Erica Sadun via swift-evolution \<swift-evolution@swift.org\> wrote: While I'm not convinced of the utility or lack thereof, wouldn't it make more sense to extend `forEach` to support optionals in a similar way that map and flapMap currently…

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.

---

_[View the full topic](https://forums.swift.org/t/mutating-t-with-a-default-value-or-returning-inout-or-extending-any/40593)._
