<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="">I (feel like I) understand the generator example with yield. It is like return, but if you call it again, it picks up from where it left off. Or at least that is my reading of what it does.<div class=""><br class=""></div><div class="">I am finding that I DO NOT understand beginAsync. At first I thought it was the async/await version of 'do{}’, but as others are giving examples which use it in different ways, I find I don’t actually know how it works… because the examples aren’t using it that way. </div><div class=""><br class=""></div><div class="">Can anyone give a layman’s explanation of beginAsync. That might actually help with the bike shedding. If you can’t explain something simply, then you don’t really understand it… and if you don’t understand it, you can’t really give it an intuitive name.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon <br class=""><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 20, 2017, at 10:58 PM, David Hart 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=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello,<div class=""><br class=""></div><div class="">Thanks for the great work on the async/await proposal! After reading it, I have a few questions and comments about it, so I’m creating this thread to concentrate on that topic (instead of Actors).</div><div class=""><br class=""></div><div style="font-size: 15px;" class=""><b class="">Generators</b></div><div class=""><br class=""></div><div class="">The proposal mentions in <b class="">Problem 6</b> of the <b class="">Motivation</b> how generators can help write sequences:</div><div class=""><br class=""></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">In contrast, languages that have generators allow you to write something more close to this:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">getSequence</span>() <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">AnySequence</span><<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">Int</span>> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> seq <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> sequence {
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">for</span> i <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">in</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">1</span><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">...</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">10</span> {
<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">yield</span>(i<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">*</span>i)
}
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">AnySequence</span>(seq)
}</pre></div><div class=""><br class=""></div><div class="">This feels very similar to me from C# where the <font face="Menlo" class="">yield</font> keyword is used to support the generator feature. But I fail to see how the coroutines as described in this proposal resolve this problem. Can someone explain?</div><div class=""><br class=""></div><div style="font-size: 15px;" class=""><b class="">beginAsync</b></div><div class=""><br class=""></div><div class="">The documentation of the <font face="Menlo" class="">beginAsync</font> and <font face="Menlo" class="">suspendAsync</font> functions state:</div><div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);" class=""><span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);">// NB: Names subject to bikeshedding. These are low-level primitives that most</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> users should not need to interact with directly, so namespacing them</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> and/or giving them verbose names unlikely to collide or pollute code</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> completion (and possibly not even exposing them outside the stdlib to begin</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> with) would be a good idea.</span></pre><div class=""><br class=""></div></div><div class="">But I don’t understand how they can be kept private to the standard library when they are used for the important pattern of spawning off an async operation from a non-async function:</div><div class=""><br class=""></div><div class=""><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Despite these problems, it is essential that the model encompasses this pattern, because it is a practical necessity in Cocoa development. With this proposal, it would look like this:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">@IBAction</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">buttonDidClick</span>(<span class="pl-smi" style="box-sizing: border-box;"><span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">sender</span></span>:<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">AnyObject</span>) {
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> 1</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"></span> beginAsync {
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> 2</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"></span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">let</span> image <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> await <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">processImage</span>()
imageView.<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">image</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> image
}
<span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box;">//</span> 3</span></pre></div><div style="font-size: 15px;" class=""><b class="">Futures</b></div></div><div class=""><br class=""></div><div class="">When discussing futures, the proposal states:</div><div class=""><br class=""></div><div class=""><span style="color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">The exact design for a future type deserves its own proposal, but a proof of concept could look like this:</span></div><div class=""><span style="color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><font color="#24292e" class="">Does that sentence imply that the Core Team would welcome a Future implementation into the Standard Library?</font></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><font color="#24292e" class=""><br class=""></font></span></div><div style="font-size: 15px;" class=""><span style="background-color: rgb(255, 255, 255);" class=""><font color="#24292e" class=""><b class="">async as a subtype of throws instead of orthogonal to it</b></font></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><font color="#24292e" class=""><br class=""></font></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><font color="#24292e" class="">I’ve been thinking a lot about this since the proposal came out and I see a few serious </font></span><font color="#24292e" class="">disadvantages at making async a subtype of throws which might benefit from being discussed or/and mentioned in the proposal.</font></div><div class=""><font color="#24292e" class=""><br class=""></font></div><div class=""><font color="#24292e" class="">1. We loose the automatic documentation <font face="Menlo" class="">try</font> provides for signaling failable functions:</font></div><div class=""><br class=""></div><div class=""><font color="#24292e" face="Menlo" class="">let image = await downloadImage()</font></div><div class=""><font color="#24292e" face="Menlo" class="">let processedImage = await processImage(image)</font></div><div class=""><font color="#24292e" face="Menlo" class="">await present(MyViewController(image: image))</font></div><div class=""><font color="#24292e" class=""><br class=""></font></div><div class=""><font color="#24292e" class="">In my example, <font face="Menlo" class="">downloadImage</font> can fail because of network conditions, <font face="Menlo" class="">processImage</font> can not fail, and <font face="Menlo" class="">present</font> is the UIKit function which presents view controllers and it can’t fail either. But that’s not obvious from reading the code. We’ve lost information.</font></div><div class=""><font color="#24292e" class=""><br class=""></font></div><div class=""><font color="#24292e" class="">2. Supporting <font face="Menlo" class="">try?</font> and <font face="Menlo" class="">try!</font> adds a lot of confusion:</font></div><div class=""><font color="#24292e" class=""><br class=""></font></div><div class=""><font color="#24292e" class="">As was mentioned by Karim Nassar is another post, if <font face="Menlo" class="">await</font> infers <font face="Menlo" class="">try</font>, then there seems to be no good solution for supporting <font face="Menlo" class="">try?</font> and <font face="Menlo" class="">try!:</font></font></div><div class=""><font color="#24292e" class=""><font face="Menlo" class=""><br class=""></font></font></div><div class=""><ul class="MailOutline"><li class=""><font color="#24292e" class="">Using <font face="Menlo" class="">await?</font> and <font face="Menlo" class="">await!</font> seems slightly conter-intuitive because we are further mixing the concepts of coroutines/asynchronous operations with error handling.</font></li><li class=""><font color="#24292e" class="">Adding <font face="Menlo" class="">try?</font> and <font face="Menlo" class="">try!</font> (like suggested Chris Lattner) feels like it makes point (1) by having both explicit <font face="Menlo" class="">try?/! </font>and implicit <font face="Menlo" class="">try</font> through <font face="Menlo" class="">await</font>.</font></li></ul><div class=""><font color="#24292e" class=""><br class=""></font></div></div><div class=""><font color="#24292e" class="">3. Philosophical discussion</font></div><div class=""><font color="#24292e" class=""><br class=""></font></div><div class=""><font color="#24292e" class="">If async calls don’t return futures because coroutines are a generally useful language features beyond the domain of async/await, doesn’t making async imply throws also muddy the concept of coroutines where failable coroutine operations don’t make much sense?</font></div><div class=""><font color="#24292e" class=""><br class=""></font></div><div class=""><font color="#24292e" class="">David.</font></div></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>