[swift-evolution] [Review] Add a Lazy flatMap for Sequences of Optionals

Jordan Rose jordan_rose at apple.com
Wed Dec 16 12:12:29 CST 2015


> On Dec 16, 2015, at 0:03 , Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Dec 15, 2015, at 7:21 PM, Rob Mayoff via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Dmitri wrote:
>> What's the advantage?  Why would we want to have a type that is isomorphic to Optional, except that conforms to CollectionType?
>> 
>> My question (apologies if it has been answered already) is: Why isn't Optional a CollectionType, like it is in Scala?
> 
> I don't remember the rationale, but it used to be that way <https://github.com/apple/swift/commit/fad874708e05cff56aec5587a4b0f49cdadc6d11> and during the run-up to the Swift 1 release IIRC several members of the Swift team objected to it.  That's the answer to Dmitri's question: I have considered that to be off the table, but we could revisit it.  
> 
> One cute effect, which might be too cute for some, is that
> 
>   if let x = y {  ... } 
> 
> becomes equivalent to
> 
>   for x in y { ... }
> 
> when y is an optional.

IIRC we made it a CollectionType purely so we could use "for x in y" while "if let x = y" was being implemented. We didn't actually consider it properly.

Dmitri has mentioned at times that he's concerned about the implicit conversion to Optional kicking in when the contextual type is "T: CollectionType", but I don't think that would actually happen. We don't try random implicit conversions, only the ones that there's context for. But someone would need to verify that.

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


More information about the swift-evolution mailing list