[swift-evolution] Proposal: Add generator functions to the language

David Waite david at alkaline-solutions.com
Sat Dec 12 00:03:16 CST 2015



> On Dec 11, 2015, at 7:18 PM, Jordan Rose via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Another possible syntax, inspired by "throws".
> 
> func foo() yield -> String

Yes, I also suspect that would play better with generators from closures. 

> I'm not sure what happens if a function both yields and throws. I guess that can't happen right now because generators are required not to throw when producing their next() value.

The only way I’ve thought of so far is to have the next iteration return Either a correct value or an ErrorType - probably through a Result enum. Unlike Optional however, there doesn’t seem to be a monadic way to represent possibly errored states - you are meant to use do/try/throw/catch. 

-DW

> 
> Jordan
> 
>> On Dec 11, 2015, at 18:14 , Jordan Rose <jordan_rose at apple.com <mailto:jordan_rose at apple.com>> wrote:
>> 
>> It's desireable for the signature of a function to be fully checkable without looking at the body, for a number of reasons. It's also something clients of a library will want to know, so it needs to be displayed in the library's interface somehow.
>> 
>> Jordan
>> 
>> 
>>> On Dec 11, 2015, at 18:03 , Andrey Tarantsov via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> Do we actually need the "generator" keyword? Why can't the compiler figure it out by the presence of yield?
>>> 
>>> And "yield" is a fairly standard keyword across languages, so no harm in claiming it, IMO.
>>> 
>>> +1 on the proposal.
>>> 
>>> A.
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
> 
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/dd8b02d9/attachment.html>


More information about the swift-evolution mailing list