[swift-evolution] Brainstorming: Optional sugar inferred map

Chris Lattner clattner at apple.com
Mon Feb 15 11:45:18 CST 2016

On Feb 15, 2016, at 12:49 AM, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org> wrote:
> The ? operator is unwrapped and the appendContentsOf method is only executed if the value is non-nil. However you can not do the following, which seems natural:
> a? + b

FWIW, "a?” is the degenerate form of the optional chaining operation, which tests A, conditionally extracts its value, then re-wraps it back up without performing any other operations on it.  It is equivalent to “a?.identity()”.  The compiler rejects it because it is confusing/surprising for people, not because it is undefined.

I would be very concerned with defining this operation to mean something else, because that introduces inconsistency and a completely different kind of surprise to the language.

> To address this, the nil-coalescing operator would allow $$, where $$ is the unwrapped unnamed result of the expression when non nil: 

Just to point one thing out, ?? is an operator in the stdlib.  Doing something like this would require baking it into the compiler.

IMO, the problem you’re addressing is already adequately solved by guard.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160215/e7360af8/attachment.html>

More information about the swift-evolution mailing list