<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">+1.<div class=""><br class=""></div><div class="">I make extensive use of generators in Python, particularly for lazy evaluation, and their absence in Swift is unfortunate.</div><div class=""><br class=""></div><div class="">In a typical ARC-based iOS scenario</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">for photo in photoGenerator() {</div><div class=""> //something with photo</div><div class="">}</div></blockquote><div class=""><br class=""></div><div class="">is ("should be specified to be") more memory efficient than</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">for photo in something.buildArrayOfPhotos() {</div><div class=""> //something with photo</div><div class="">}</div><div class=""><br class=""></div></blockquote>which likely entails an OOM crash and is a common source of noob programmer error.<div class=""><br class=""></div><div class="">Reducing the friction of SequenceType in such cases is an improvement entirely orthogonal to coroutines (of which I am much more skeptical).</div><div class=""><br class=""></div><div class="">Drew</div><div class=""><br class=""><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On Dec 11, 2015, at 6:21 PM, David Waite via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Looking for feedback on crafting a proposal adding generator functions to Swift. I understand this will likely be a very involved proposal at the language level, although I actually don’t know the complexity of the change within the Swift compiler itself.<div class=""><div class=""><br class=""></div><div class="">This would be a function which returns multiple values, which is converted by the compiler to a function returning a SequenceType<div class=""><br class=""></div><div class="">A very basic syntax would be to add generator as a modifier to func, and likely involve a new keyword ‘yield’ to differentiate from the flow control behavior of ‘return’.</div><div class=""><br class=""></div><div class="">So for example:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">generator func helloGenerator(name:String?) -> String {</div><div class=""> yield “Hello”</div><div class=""> yield name ?? “World”</div><div class="">}</div></blockquote><div class=""><br class=""></div><div class="">Would have the following expected usage:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">for str in helloGenerator(“David") {</div><div class=""> print str</div>// prints:</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">// Hello<br class=""><div class="">// David</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">}</div></blockquote><br class=""><div class=""><div class="">And for those unfamiliar to these sorts of simple cases, would have equivalent behavior to the following code:</div><div class=""><br class=""></div><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 45> </span>func helloGenerator(name:String?) -> HelloGenerator { </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 46. </span> return HelloGenerator(name) </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);" class=""> 47. <span style="" class="">} </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);" class=""> 48. <span style="" class=""> </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 49. </span>struct HelloGenerator : GeneratorType, SequenceType { </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 50. </span> var position:Int = 0 </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 51. </span> let name:String? </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);" class=""> 52. <span style="" class=""> </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 53. </span> private init(_ name:String?) { </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 54. </span> self.name = name </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 55. </span> } </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);" class=""> 56. <span style="" class=""> </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 57. </span> func generator() -> HelloGenerator {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 58. </span> return self </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 59. </span> } </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 60. </span> </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 61. </span> mutating func next() -> String? { </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 62. </span> switch position { </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 63. </span> case 0: </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 64. </span> position = 1 </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 65. </span> return "Hello" </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 66. </span> case 1: </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 67. </span> position = 2 </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 68. </span> return name ?? "World" </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 69. </span> default: </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 70. </span> return nil </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 71. </span> } </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class=""> 72. </span> } </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);" class=""> 73. <span style="" class="">} </span></div></div><div class=""><br class=""></div></blockquote>This syntax has at a bare minimum issues with generator closures and for a terse syntax for yielding over another sequence type within a generator function vs. using a loop. (possibly “yield in sequenceName”)</div><div class=""><br class=""></div><div class="">The interaction with the error system might involve disallowing throws from generator functions, or having the Element type be a Result<T> rather than T, as the GeneratorType next() method is not declared as throwing.</div><div class=""><br class=""></div><div class=""><div class="">This could pair well to make for-in loops more comprehensive, especially if C-style for loops are eliminated.</div></div><div class=""><br class=""></div><div class="">This would possibly be a first step toward a coroutine-based concurrency system, although I am not proposing that sort of usage or scope here. The goal would be to emit an object compatible with SequenceType</div></div><div class=""><br class=""></div><div class="">-David Waite (DW)</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=OtwgWwWn2mvmck2XIGZZg64wEmoo4f4ILYe4SqMqwHpzhBKcifqhaRhiL43vxj-2FP66fOkV9oHtnihimNBJTeoNEtUGYY2ToaB0FbtUQhcf-2BPqoA4DPy2wrCIC3HyK0i4H058lIBTVuAUraJqEbSA41qS0V9CtRSdGODyogZfk4Hf3jon4-2BTga5d1jBkjsC8qc9wnuEzqRxcJx-2FZPOO9Vg-2FalswESzM4kCx-2Fah4Zi3g4-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></body></html>