[swift-evolution] Using `map` and `flatMap` with implicit optionals

Tim Vermeulen tvermeulen at me.com
Mon Oct 24 09:53:38 CDT 2016


This could cause problems when the underlying type also has a `map` or `flatMap` function:

let array: [Int]! = nil
array.map { $0 } // crash

Should this use Optional.map or Array.map?

As a workaround, you can simply do

let array: [Int]! = nil
(array as Optional).map { $0 } // nil

> Since `ImplicitlyUnwrappedOptional` types can be unwrapped with an `if let` or
> accessed as an optional as in `foo?.bar` then why shouldn't there be implementations of `map` or `flatMap` for use with IUO types?
> 
> I think having `map` and `flatMap` available on types marked with "!" would be convenient
> but is there a good reason why that isn't the case now?
> 
> 
> 
> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161024/05bf6f77/attachment.html>


More information about the swift-evolution mailing list