<div dir="ltr">Presumably, this could work with (or replace) GeneratorType, since all generators as they currently exist could be implemented this way.<div class="gmail_extra">
<br><div class="gmail_quote">On Fri, Dec 11, 2015 at 4:36 PM, Drew Crawford via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">+1.<div><br></div><div>I make extensive use of generators in Python, particularly for lazy evaluation, and their absence in Swift is unfortunate.</div><div><br></div><div>In a typical ARC-based iOS scenario</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>for photo in photoGenerator() {</div><div>    //something with photo</div><div>}</div></blockquote><div><br></div><div>is  (&quot;should be specified to be&quot;) more memory efficient than</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>for photo in something.buildArrayOfPhotos() {</div><div>  //something with photo</div><div>}</div><div><br></div></blockquote>which likely entails an OOM crash and is a common source of noob programmer error.<div><br></div><div>Reducing the friction of SequenceType in such cases is an improvement entirely orthogonal to coroutines (of which I am much more skeptical).</div><div><br></div><div>Drew</div><div><br><div><br></div><div><div><blockquote type="cite"><div><div class="h5"><div>On Dec 11, 2015, at 6:21 PM, David Waite via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br></div></div><div><div><div class="h5"><div style="word-wrap:break-word">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><div><br></div><div>This would be a function which returns multiple values, which is converted by the compiler to a function returning a SequenceType<div><br></div><div>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><br></div><div>So for example:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>generator func helloGenerator(name:String?) -&gt; String {</div><div>    yield “Hello”</div><div>    yield name ?? “World”</div><div>}</div></blockquote><div><br></div><div>Would have the following expected usage:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>for str in helloGenerator(“David&quot;) {</div><div>   print str</div>// prints:</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">//    Hello<br><div>//    David</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>}</div></blockquote><br><div><div>And for those unfamiliar to these sorts of simple cases, would have equivalent behavior to the following code:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 45&gt; </span>func helloGenerator(name:String?) -&gt; HelloGenerator { </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 46. </span>  return HelloGenerator(name) </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(169,169,169)"> 47. <span>} </span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(169,169,169)"> 48. <span> </span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 49. </span>struct HelloGenerator : GeneratorType, SequenceType { </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 50. </span>   var position:Int = 0 </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 51. </span>   let name:String? </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(169,169,169)"> 52. <span> </span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 53. </span>   private init(_ name:String?) { </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 54. </span>      <a href="http://self.name" target="_blank">self.name</a> = name </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 55. </span>   } </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(169,169,169)"> 56. <span>    </span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 57. </span>   func generator() -&gt; HelloGenerator {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 58. </span>       return self </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 59. </span>   } </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 60. </span>     </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 61. </span>   mutating func next() -&gt; String? { </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 62. </span>      switch position { </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 63. </span>      case 0: </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 64. </span>          position = 1 </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 65. </span>          return &quot;Hello&quot; </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 66. </span>      case 1: </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 67. </span>          position = 2 </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 68. </span>          return name ?? &quot;World&quot; </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 69. </span>      default: </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 70. </span>          return nil </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 71. </span>      } </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#a9a9a9"> 72. </span>   } </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(169,169,169)"> 73. <span>} </span></div></div><div><br></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><br></div><div>The interaction with the error system might involve disallowing throws from generator functions, or having the Element type be a Result&lt;T&gt; rather than T, as the GeneratorType next() method is not declared as throwing.</div><div><br></div><div><div>This could pair well to make for-in loops more comprehensive, especially if C-style for loops are eliminated.</div></div><div><br></div><div>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><br></div><div>-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="min-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">
</div></div></div>
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=P-2BsYbBZHRBuLDBJaL4DIKDNfkkjpROowTyRAObV11qwktWSuZWlRb-2FBX1B13IQGrn3jZ8YVzc69b7a7AH5ERkiGIrePE2JQ-2F-2FaGJiiyCxzFnMqNVk58E1UelozSSL49krHY0TBwIVy-2FDDGP66SqZ686b8Gc8moMCkHv2QWfn-2B0QQ5vRxSKuzf4ftwZ6k3lop11b7CbC7fRQBqmPbVYXNBdW8Tt85kD88A2t6LPOu4FM-3D" alt="" width="1" height="1" border="0" style="min-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">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>