[swift-evolution] Passing an optional first argument to sequence(first:next:)

Jordan Rose jordan_rose at apple.com
Thu Aug 25 13:45:12 CDT 2016


> On Aug 20, 2016, at 02:26, Tim Vermeulen <tvermeulen at me.com> wrote:
> 
> What you’re saying makes sense, and I might not have brought this up in the first place if `first.map { sequence(first: $0, next: next } ?? []` worked. The main annoyance is that the best solution (currently) seems to be to copy the source code and make a change.
> 
> (cc-ing Jordan Rose because of a related swift-users thread) This might be a bit of a stretch, but can’t Swift upcast sequences to AnySequence implicitly, like is done with AnyHashable? That would make `first.map { sequence(first: $0, next: next } ?? []` instantly valid, I think. There’s also something to be said for consistency between type erasers. (I’m not necessarily talking about Swift 3)

Sorry to let this sit. I really would not want to implement this by upcasting to AnySequence. That would be tremendously inefficient.

In general, AnyHashable is special because of dictionary literals, where the same thing would have to be specified over and over again. That’s not true here. (And we weren’t even going to do it for AnyHashable originally.)

I’m mildly in favor of the very original form of this proposal,

func sequence(first: T?, next: @escaping (T) -> T?) -> UnfoldFirstSequence<T>

It’s also very nearly an additive change; the only existing code that would change meaning is that IUOs would not cause traps. (And the contrived examples with almost no type information, where the user is currently trying to infer T = Optional<X> and return a sequence of Optional<X>.)

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


More information about the swift-evolution mailing list