[swift-evolution] Brainstorming: Optional sugar inferred map

Alan Skipp al_skipp at icloud.com
Tue Feb 16 17:54:58 CST 2016


> On 16 Feb 2016, at 14:10, tvermeulen via swift-evolution <swift-evolution at swift.org> wrote:
> 
> let a: String = "123"
> let b: String? = "456"
> let c = a.stringByAppendingString(b?)
> 
> This, however, does not work.

If ‘b’ were nil then the result of appending it to a String would be nil. I think this would cause quite a bit of confusion (an appending operation resulting in less than what you started with).

The equivalent code would be:

b.map { a.stringByAppendingString($0) }

It might not be as succinct, but it makes it clearer that the operation depends on the value of ‘b’.


More information about the swift-evolution mailing list