<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="">One difference is the memory usage:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">func helloGenerator() -> LazyCollection<[String]> {</div><div class=""> return [</div><div class=""> 1GBAllocation(),</div><div class=""> 1GBAllocation()</div><div class=""> ].lazy</div><div class="">}</div></blockquote><div class=""><br class=""></div>vs<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">func helloGenerator() -> SequenceType {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>yield 1GBAllocation()</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>//allocation released</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>yield 1GBAllocation()</div><div class="">}</div></blockquote><div class=""><br class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 11, 2015, at 7:38 PM, Kametrixom Tikara 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=""><span style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">What exactly is the difference to just returning a sequence?</span><div class="" style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""><span class="" style="color: rgb(53, 86, 138);">func</span><span class="Apple-converted-space"> </span>helloGenerator(name :<span class="Apple-converted-space"> </span><span class="" style="color: rgb(195, 89, 0);">String</span>?) -> [<span class="" style="color: rgb(195, 89, 0);">String</span>] {</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(53, 86, 138);">return</span><span class="Apple-converted-space"> </span>[</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(232, 35, 0);">"Hello"</span>,</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> name ??<span class="Apple-converted-space"> </span><span class="" style="color: rgb(232, 35, 0);">"World"</span></font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> ]</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class="">}</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 16px;"><font size="1" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(88, 126, 168);"><font size="1" class=""><span class="" style="color: rgb(53, 86, 138);">for</span><span class="" style=""><span class="Apple-converted-space"> </span>str<span class="Apple-converted-space"> </span></span><span class="" style="color: rgb(53, 86, 138);">in</span><span class="" style=""><span class="Apple-converted-space"> </span></span>helloGenerator<span class="" style="">(</span><span class="" style="color: rgb(232, 35, 0);">"David"</span><span class="" style="">) {</span></font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(88, 126, 168);">print</span>(str)</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class="">}</font></div><div class=""><br class=""></div><div class="">And if you want if lazy:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""><span class="" style="color: rgb(53, 86, 138);">func</span><span class="Apple-converted-space"> </span>helloGenerator(name :<span class="Apple-converted-space"> </span><span class="" style="color: rgb(195, 89, 0);">String</span>?) -><span class="Apple-converted-space"> </span><span class="" style="color: rgb(195, 89, 0);">LazyCollection</span><[<span class="" style="color: rgb(195, 89, 0);">String</span>]> {</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(53, 86, 138);">return</span><span class="Apple-converted-space"> </span>[</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(232, 35, 0);">"Hello"</span>,</font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> name ??<span class="Apple-converted-space"> </span><span class="" style="color: rgb(232, 35, 0);">"World"</span></font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class=""> ].<span class="" style="color: rgb(88, 126, 168);">lazy</span></font></div><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><font size="1" class="">}</font></div></div><div class=""><br class=""></div><div class="">Seems like exactly the same, except it doesn’t need two new keywords nor revamping of SequenceType/GeneratorType</div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 12 Dec 2015, at 01:21, 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 class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">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 class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><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 class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class="">for str in helloGenerator(“David") {</div><div class=""> print str</div>// prints:</blockquote><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;">// Hello<br class=""><div class="">// David</div></blockquote><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><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 class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 45><span class="Apple-converted-space"> </span></span>func helloGenerator(name:String?) -> HelloGenerator { </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 46.<span class="Apple-converted-space"> </span></span> return HelloGenerator(name) </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);"> 47.<span class="Apple-converted-space"> </span><span class="">} </span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);"> 48.<span class="Apple-converted-space"> </span><span class=""> </span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 49.<span class="Apple-converted-space"> </span></span>struct HelloGenerator : GeneratorType, SequenceType { </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 50. </span><span class="Apple-converted-space"> </span> var position:Int = 0 </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 51. </span><span class="Apple-converted-space"> </span> let name:String? </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);"> 52.<span class="Apple-converted-space"> </span><span class=""> </span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 53. </span><span class="Apple-converted-space"> </span> private init(_ name:String?) { </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 54.<span class="Apple-converted-space"> </span></span> self.name = name </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 55. </span><span class="Apple-converted-space"> </span> } </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);"> 56. <span class=""><span class="Apple-converted-space"> </span> </span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 57. </span><span class="Apple-converted-space"> </span> func generator() -> HelloGenerator {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 58. </span><span class="Apple-converted-space"> </span> return self </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 59. </span><span class="Apple-converted-space"> </span> } </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 60.<span class="Apple-converted-space"> </span></span> </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 61. </span><span class="Apple-converted-space"> </span> mutating func next() -> String? { </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 62.<span class="Apple-converted-space"> </span></span> switch position { </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 63.<span class="Apple-converted-space"> </span></span> case 0: </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 64.<span class="Apple-converted-space"> </span></span> position = 1 </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 65.<span class="Apple-converted-space"> </span></span> return "Hello" </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 66.<span class="Apple-converted-space"> </span></span> case 1: </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 67.<span class="Apple-converted-space"> </span></span> position = 2 </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 68.<span class="Apple-converted-space"> </span></span> return name ?? "World" </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 69.<span class="Apple-converted-space"> </span></span> default: </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 70.<span class="Apple-converted-space"> </span></span> return nil </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 71.<span class="Apple-converted-space"> </span></span> } </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(169, 169, 169);"> 72. </span><span class="Apple-converted-space"> </span> } </div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);"> 73.<span class="Apple-converted-space"> </span><span 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=UMghxGHOvTEcVGG3PKHzuJe-2F1jwKPcDcSNH0SiRhP154FpbHyH8LgfGq4t4pY0jlEzFyN3HG0284UFBybtPAVRsupOwEhsCN2pXeRkqc831KfClDufYK5Kl4WNqtWpBbjzIyzXRbhMZmZppGDt9RYzBk3yCacZhYgXUJQK4FGfP6ZgzUedIBjcHAlKZlb5Z5kMydnZo9wpG8pAM77QPtBU-2Bgl-2BMjyQGxgf-2FwNnwzxdc-3D" alt="" width="1" height="1" border="0" class="" style="height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=OtwgWwWn2mvmck2XIGZZg64wEmoo4f4ILYe4SqMqwHo-2FkLZyk2RNPKuwforFF7og6YiVmS0ctELkDVzI-2F90ZxirsnuhwhxRSNT-2BDDayU63coKKdFVPYINjql1quhK8yJp4dzqgjUmHOlDAIa0yyFHabud14uohaIaTwirmgDABwp2TRDRwIIN4A8udF9-2BAQnKXT5MGCLsamy-2BLiFHVs2ahzPbG9Fg3TjJxEERYtaYZg-3D" alt="" width="1" height="1" border="0" style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space"> </span>_______________________________________________</span><br style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: HelveticaNeue; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></div></body></html>