<div dir="ltr"><div>Before i make a comment on this topic i need to confirm something:</div><div><br></div><div>On original proposal, this:</div><div><br></div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-radius:3px;word-break:normal;color:rgb(36,41,46)"><span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="inbox-inbox-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">processImageData1</span>(<span class="inbox-inbox-pl-smi" style="box-sizing:border-box"><span class="inbox-inbox-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">completionBlock</span></span>: (result: Image) <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span> <span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Void</span>) {
<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loadWebResource</span>(<span class="inbox-inbox-pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="inbox-inbox-pl-pds" style="box-sizing:border-box">"</span>dataprofile.txt<span class="inbox-inbox-pl-pds" style="box-sizing:border-box">"</span></span>) { dataResource <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">in</span>
<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loadWebResource</span>(<span class="inbox-inbox-pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="inbox-inbox-pl-pds" style="box-sizing:border-box">"</span>imagedata.dat<span class="inbox-inbox-pl-pds" style="box-sizing:border-box">"</span></span>) { imageResource <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">in</span>
<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">decodeImage</span>(dataResource, imageResource) { imageTmp <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">in</span>
<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">dewarpAndCleanupImage</span>(imageTmp) { imageResult <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">in</span>
<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">completionBlock</span>(imageResult)
}
}
}
}
}</pre></div><div><br></div><div>Is converted to this:</div><div><br></div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-radius:3px;word-break:normal;color:rgb(36,41,46)"><span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="inbox-inbox-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">processImageData1</span>() async <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span> Image {
<span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> dataResource <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loadWebResource</span>(<span class="inbox-inbox-pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="inbox-inbox-pl-pds" style="box-sizing:border-box">"</span>dataprofile.txt<span class="inbox-inbox-pl-pds" style="box-sizing:border-box">"</span></span>)
<span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> imageResource <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">loadWebResource</span>(<span class="inbox-inbox-pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="inbox-inbox-pl-pds" style="box-sizing:border-box">"</span>imagedata.dat<span class="inbox-inbox-pl-pds" style="box-sizing:border-box">"</span></span>)
<span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> imageTmp <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">decodeImage</span>(dataResource, imageResource)
<span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> imageResult <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">dewarpAndCleanupImage</span>(imageTmp)
<span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">return</span> imageResult
}</pre></div><br><br>And has other sentences explain that await will not block the thread etc...<div><br></div><div>So for me, this:</div><div><br></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px;color:rgb(33,33,33);font-size:13px"><div>func OnButtonClicked(_ sender:AnyObject) {</div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px;color:rgb(33,33,33);font-size:13px"><div> let button = sender as! UIButton</div><div> button.isEnabled = false</div><div> beginAsync {</div><div> await DoSomethingAsync()</div><div> button.isEnabled = true</div><div> }</div><div>}</div></blockquote></div><div><br></div><div>Does not make any sense... Because await will not block the thread we can assume (like the exemple on proposal) that compiler will actually do the same what we will do using GDC and pick everyone to until the context ends and encapsulate as completion handle to the away call.</div><div><br></div><div>So we will only have this:</div><div><br></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px;color:rgb(33,33,33);font-size:13px">func OnButtonClicked(_ sender:AnyObject) {</blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px;color:rgb(33,33,33);font-size:13px"> let button = sender as! UIButton<br></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px;color:rgb(33,33,33);font-size:13px"> button.isEnabled = false</blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px;color:rgb(33,33,33);font-size:13px"> await DoSomethingAsync()</blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px;color:rgb(33,33,33);font-size:13px"> button.isEnabled = true<span style="color:rgb(0,0,0);font-size:small"> </span></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px;color:rgb(33,33,33);font-size:13px">}<br></blockquote><div><br></div><div>I don't know how to handle reentrante or something like that, but the proposal give me the impression this behavior.</div></div><div><br></div><div>In the other way, await will block the thread!</div><div><br></div><div>I know the proposal give us this exemple:</div><div><br></div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-radius:3px;word-break:normal;color:rgb(36,41,46)"><span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">@IBAction</span> <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="inbox-inbox-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">buttonDidClick</span>(<span class="inbox-inbox-pl-smi" style="box-sizing:border-box"><span class="inbox-inbox-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">sender</span></span>:<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">AnyObject</span>) {
<span class="inbox-inbox-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="inbox-inbox-pl-c" style="box-sizing:border-box">//</span> 1</span>
<span class="inbox-inbox-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> beginAsync {
<span class="inbox-inbox-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="inbox-inbox-pl-c" style="box-sizing:border-box">//</span> 2</span>
<span class="inbox-inbox-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> image <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">processImage</span>()
imageView.<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">image</span> <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> image
}
<span class="inbox-inbox-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="inbox-inbox-pl-c" style="box-sizing:border-box">//</span> 3</span>
<span class="inbox-inbox-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span>}</pre></div><div><br></div><div><br></div><div>But this is different, because "3" will be fired before "2".</div><div><br></div><div>And this one:</div><div><br></div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-radius:3px;word-break:normal;color:rgb(36,41,46)"><span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">@IBAction</span> <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="inbox-inbox-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">buttonDidClick</span>(<span class="inbox-inbox-pl-smi" style="box-sizing:border-box"><span class="inbox-inbox-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">sender</span></span>:<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">AnyObject</span>) {
beginAsync {
<span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">let</span> image <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> await <span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">processImageData</span>()
<span class="inbox-inbox-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="inbox-inbox-pl-c" style="box-sizing:border-box">//</span> Do the update on the main thread/queue since it owns imageView.</span>
<span class="inbox-inbox-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"></span> mainQ.<span class="inbox-inbox-pl-smi" style="box-sizing:border-box">async</span> {
imageView.<span class="inbox-inbox-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">image</span> <span class="inbox-inbox-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> image
}
}
}</pre></div><div><br></div><div><br></div><div>Does not make any sense too.</div><div><br></div><div>The only way to <span style="background-color:rgb(246,248,250);color:rgb(36,41,46);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px">await</span> to do not block the thread is make a early return like beginAsync and encapsulate as completion handle the rest of the code. </div><div><br></div><div>If not, i don't see how the first exemple of the proposal will be made.</div><div><br><div class="gmail_quote"><div dir="ltr">Em qua, 8 de nov de 2017 às 15:55, Adam Kemp via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> escreveu:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div>(I changed the subject since it’s not really the same topic)</div><div><br></div><div>The short answer is that this code has the same problem even without async void:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>func sendMessage() {</div><div> beginAsync {</div><div> // …</div><div> }</div><div>}</div><div><font color="#5856d6"><br></font></div><div>func onButtonClick() {</div><div><span class="m_-2521987769368567525Apple-tab-span" style="white-space:pre-wrap">        </span>sendMessage()</div><div><span class="m_-2521987769368567525Apple-tab-span" style="white-space:pre-wrap">        </span>showAlert("message sent")</div><div><div dir="ltr"><div>}</div></div></div></blockquote><div><br></div><div>If you want someone to be able to wait for something to finish then you make your function awaitable. So what does that mean for an async function that doesn’t really return anything? To understand what I’m thinking it helps to understand how C# does it, since that’s the model I’m proposing, and I think it works really well. In C# it works like this:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>void NoReturn() { }</div><div>int IntReturn() { return 0; }</div><div><br></div><div>async Task NoReturnAsync() { await Task.Yield(); }</div><div>async Task<int> IntReturnAsync() { await Task.Yield(); return 0; }</div><div><br></div><div>async void NoReturnAsyncVoid() { await Task.Yield(); }</div><div><br></div><div>async Task Caller()</div><div>{</div><div> NoReturn();</div><div> int i = IntReturn();</div><div> await NoReturnAsync();</div><div> int j = await IntReturnAsync();</div><div><br></div><div> NoReturnAsync(); // this cannot be awaited</div><div>}</div></blockquote><div><br></div>An important difference is that in C# the async keyword does not make a function awaitable. Notice how NoReturnAsyncVoid is marked as “async”, but the caller cannot use “await” with it. So what do you do if you want to wait for it to finish, like in your example? Well notice that another function NoReturnAsync doesn’t actually appear to return anything, even though its return type is Task. The compiler transforms the function into pieces where the first piece (the one actually called by the caller) returns a Task object. A bare Task is only used for waiting for completion, whereas a Task<T> also holds a return value. When you make a function that returns T async then you change the return type to Task<T>. When you make a void function async and want to allow the caller to wait for it to finish then you change the void to Task. When you make a void function async but want the caller to not wait for it to finish then you leave it as void.<div><br></div><div>This is subtle, but consider this alternative form:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>void NoReturn() { }</div><div>int IntReturn() { return 0; }</div><div><br></div><div>Task NoReturnAsync() { return Task.Yield(); }</div><div>Task<int> IntReturnAsync() { return Task.Yield(); return 0; }</div><div><br></div><div>void NoReturnAsyncVoid() { Task.Yield(); }</div><div><br></div><div>async Task Caller()</div><div>{</div><div> NoReturn();</div><div> int i = IntReturn();</div><div> await NoReturnAsync();</div><div> int j = await IntReturnAsync();</div><div><br></div><div> NoReturnAsync(); // this cannot be awaited</div><div>}</div></div></blockquote><div><br></div>I changed all of the “async" functions above except for Caller by removing the “async” keyword and (where applicable) added return statements. Now none of those functions is async, but they are functionally equivalent. Notice that the async Caller (which I didn’t change at all) can still await the ones that return Task or Task<T>. That’s because, again, in C# the async keyword does not mean “this can be awaited”. Being able to await a function call is orthogonal to whether that function is marked as async.<div><br></div><div>Async means only one thing: that function can use the await keyword, and it will be decomposed by the compiler accordingly. So what can you await? Anything that’s “awaitable”. :) Basically if the return type of the function has a GetAwaiter method that returns a type that has a few properties and methods then you can use await on that function. This is described here: <<a href="https://blogs.msdn.microsoft.com/pfxteam/2011/01/13/await-anything/" target="_blank">https://blogs.msdn.microsoft.com/pfxteam/2011/01/13/await-anything/</a>>. Task happens to have a GetAwaiter (<<a href="https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.getawaiter(v=vs.110).aspx" target="_blank">https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.getawaiter(v=vs.110).aspx</a>>) method that returns a TaskAwaiter (<<a href="https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.taskawaiter(v=vs.110).aspx%3E)" target="_blank">https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.taskawaiter(v=vs.110).aspx>)</a>, which happens to have an IsCompleted property and GetResult and OnCompleted methods.</div><div><br></div><div>You can make any type awaitable by supplying a suitable GetAwaiter implementation, even using an extension method. I think this could probably be done with protocols and extensions in Swift.</div><div><br></div><div>Back to the problem you described: wouldn’t an async void method be confusing to callers who expect it to finish before returning? Obviously if it’s a function that requires the caller to wait before continuing then it should be made awaitable. So in C# you wouldn’t make an async void function that you expected people to want to wait for, and in C# you have that choice: you can make an awaitable function that returns nothing to the awaiter.</div><div><br></div><div>The question, though, is “are there any use cases for an async function that doesn’t require its caller to wait for it?” Or, put another way, is there a use case for an async function that a caller can call as if it’s just any other void function? There definitely are multiple use cases for that, but the most important one probably is event callbacks, most often UI event callbacks. Think of a button click handler. When a button is clicked maybe you want to start some async task. This is a common pattern in .Net:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>private async void OnButtonClicked(object sender, EventArgs e)</div><div>{</div><div> Button button = (Button)sender;</div><div> button.IsEnabled = false;</div><div> await DoSomethingAsync();</div><div> button.IsEnabled = true;</div><div>}</div></blockquote><div><br></div>In Swift with the current proposal that would be something like this:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>func OnButtonClicked(_ sender:AnyObject) {</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div> let button = sender as! UIButton</div><div> button.isEnabled = false</div><div> beginAsync {</div><div> await DoSomethingAsync()</div><div> button.isEnabled = true</div><div> }</div><div>}</div></blockquote><div><br></div>If you compare those two implementations I think it’s obvious which one is clearer. The advantage of async/await is that it makes async code look like sync code by preserving the logical code flow. It makes the compiler do the hard work of splitting the function up. If you have to use beginAsync then you’re forced to do what the compiler could do for you. You have to think about where it goes, and how much should be inside that block or outside. Someone might be tempted to write that method above like this:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">func OnButtonClicked(_ sender:AnyObject) {</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"> let button = sender as! UIButton<br> button.isEnabled = false</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"> beginAsync {</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"> await DoSomethingAsync()<br> }</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"> button.isEnabled = true<br><div>}</div></blockquote><div><br></div>That code would be wrong. Is that obvious at a glance? I don’t think so. What about this?<div><br></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">func OnButtonClicked(_ sender:AnyObject) {</blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"> beginAsync {</blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"> let button = sender as! UIButton<br> button.isEnabled = false</blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"> await DoSomethingAsync()</blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"> button.isEnabled = true<br> }</blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>}</div></blockquote></div><div><br></div><div>That code does the right thing. So why wouldn’t you always write it that way? Serious question: what is the use case for using beginAsync for only part of a function? It looks like every example in Chris’s proposal wraps the entire contents of the function (except for one in an initializer).</div><div><br></div><div>If beginAsync is used almost exclusively to wrap the entire contents of void functions that you want to be async then why wouldn’t we just make it possible to make that function itself async and use await directly? It seems much clearer to me. async/await is already kind of confusing for newcomers, and I think beginAsync makes it worse by introducing a new concept that is easily misused and shouldn’t be necessary.</div><div><div><div><div><div><div><div><div><br></div><div><blockquote type="cite"><div>On Nov 7, 2017, at 10:22 PM, omochi.metaru <<a href="mailto:omochi.metaru@gmail.com" target="_blank">omochi.metaru@gmail.com</a>> wrote:</div><br class="m_-2521987769368567525Apple-interchange-newline"><div><div dir="ltr"><div>I totally agree Yuta's suggestion.</div><div>beginAsync does not have to accept function which throws.</div><div><br></div><div>> Adam</div><div><br></div><div>I don't think that C# style async void function invodation matchs swift.</div><div><br></div><div>If we can do, following code can be compile.</div><div><br></div><div>```swift</div><div>async func sendMessage() -> Void { ... }</div><div><br></div><div>func onButtonClick() {</div><div><span class="m_-2521987769368567525Apple-tab-span" style="white-space:pre-wrap">        </span>sendMessage()</div><div><span class="m_-2521987769368567525Apple-tab-span" style="white-space:pre-wrap">        </span>showAlert("message sent")</div><div>}</div><div>```</div><div><br></div><div>But in this case, the logic actually programmer desired is </div><div>showing alert after sendMessage completed.</div><div>Above style code is not easy readable about execution fall through</div><div>without waiting completion of sendMessage to showAlert.</div><div>With this rule, compiler can not help us to find such mistaken code.</div><div><br></div><div>This seems like unchecked exception problem in other languages.</div><div>Keep starting asynchronous invodation explicit suck like </div><div>throwing function invocation explicitly marked with `try` or `do`.</div></div><br><div class="gmail_quote"><div dir="ltr">2017年11月8日(水) 13:28 Adam Kemp via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I think I agree with this. beginAsync is similar to C#’s async void functions, and one of the gotchas in C# is that it is never safe to allow an exception to be thrown from an async void function. The reason is that if the exception happens after the continuation then there won’t be any application code above it to catch that exception. As a result, the built in behavior is to immediately crash the app.<br>
<br>
This is unavoidable in C# where it’s impossible to write a function that is guaranteed not to throw. The semantics of exception throwing don’t allow for that in C#.<br>
<br>
Swift has the advantage in this case of being able to statically verify that a function doesn’t throw so we can do better.<br>
<br>
So I would argue in favor of not allowing beginAsync to throw at all.<br>
<br>
FWIW, I also still think it would be better if we allowed for async void functions instead of requiring beginAsync in the first place. If I had my way then we would have async void, but an async void would not be allowed to throw.<br>
<br>
> On Nov 7, 2017, at 7:04 PM, Yuta Koshizawa via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
><br>
> Although I posted about this topic before, let me post this again<br>
> because I think it is important and I have received just few replies.<br>
> Sorry if I missed some discussion about it.<br>
><br>
> In the proposal (<br>
> <a href="https://gist.github.com/lattner/429b9070918248274f25b714dcfc7619" rel="noreferrer" target="_blank">https://gist.github.com/lattner/429b9070918248274f25b714dcfc7619</a> ),<br>
> `beginAsync` has the following signature.<br>
><br>
> ```<br>
> func beginAsync(_ body: () async throws -> Void) rethrows -> Void<br>
> ```<br>
><br>
> However, I think it is better to forbid `body` to throw errors, that<br>
> is to say, to change its signature to the following one.<br>
><br>
> ```<br>
> func beginAsync(_ body: () async -> Void) -> Void<br>
> ```<br>
><br>
> Even if `beginAsync` allows that `body` throws errors, it can rethrow<br>
> ones which are thrown before only first `await` call. In following<br>
> cases, `beginAsync` just has to make the program crash when `foo`<br>
> throws an error. It breaks safety for error handing by typed<br>
> propagation realized by `throws/try`.<br>
><br>
> ```<br>
> // throws errors asynchronously<br>
> func foo() async throws -> Int { ... }<br>
><br>
> do {<br>
> beginAsync {<br>
> let a = try await foo()<br>
> // uses `a` here<br>
> }<br>
> } catch _ {<br>
> // never reaches here<br>
> }<br>
> ```<br>
><br>
> If `beginAsync` forbid `body` to throw errors, it can be detected as a<br>
> compilation error and is possible to fix it as follows.<br>
><br>
> ```<br>
> beginAsync {<br>
> do {<br>
> let a = try await foo()<br>
> // uses `a` here<br>
> } catch _ {<br>
> // error handling<br>
> }<br>
> }<br>
> ```<br>
><br>
> And even when we want to write `try` calls in `beginAsync` before<br>
> first `await` call, those lines can be moved before the `beginAsync`<br>
> call.<br>
><br>
> ```<br>
> // before ( `beginAsync` marked with `rethrows` )<br>
> do {<br>
> beginAsync {<br>
> let a = try bar()<br>
> let b = try baz()<br>
> let c = await qux(a, b)<br>
> // uses `c` here<br>
> }<br>
> catch _ {<br>
> // error handling<br>
> }<br>
><br>
> // after ( `beginAsync` without `rethrows` )<br>
> do {<br>
> let a = try bar()<br>
> let b = try baz()<br>
> beginAsync {<br>
> let c = await qux(a, b)<br>
> // uses `c` here<br>
> }<br>
> catch _ {<br>
> // error handling<br>
> }<br>
> ```<br>
><br>
> So the functionalities of `beginAsync` seems be kept even if it forbid<br>
> `body` to throw errors.<br>
><br>
> What do you think about it?<br>
><br>
> --<br>
> Yuta<br>
> _______________________________________________<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
_______________________________________________<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div><div dir="ltr">-- <br></div><div class="m_-2521987769368567525gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>omochimetaru</div></div></div>
</div></blockquote></div><br></div></div></div></div></div></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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div></div>