<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="">Hi Roy,<div class=""><br class=""></div><div class="">I’d prefer if such special syntax would not be reserved for just async computations but used for monadic computations in general (with the async case just being a monad implementation).<div class=""><br class=""></div><div class="">-Thorsten<br class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">Am 06.12.2015 um 10:21 schrieb Roy Fu via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:</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=""><div class="">Hi all,</div><div class=""><br class=""></div><div class="">Similar to the syntax ‘do try catch throw’ and ’throws’ for error handling, I propose another syntax for handling CPS functions or async call backs:</div><div class=""><br class=""></div><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: #0433ff" class="">func</span> someFutureWrappedFunc(input: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Int</span>) <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">future</span>-> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">String</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="" class=""> </span>doSomeStaffInBackground<span style="" class="">{ (result:</span>String<span style="" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">in</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: #0433ff" class="">fulfil</span> result</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><span class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class="">}</div></span></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(4, 51, 255);" class="">do<span style="" class="">{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class=""><span style="" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="" class=""> resultX = </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">perform</span><span style="" class=""> </span>someFutureWrappedFunc<span style="" class="">(</span>param<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: #0433ff" class="">let</span> resultY = <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">perform</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">anotherFutureWrappedFunc</span>(resultX)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(4, 51, 255);" class="">let</span> final = resultX + resultY + <span style="color: rgb(4, 51, 255);" class="">perform</span> <span style="color: rgb(52, 149, 175);" class="">justAnotherFutureWrappedFunc</span>(resultX)</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">someLabel</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">text</span> = final</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">} <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">timeout</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> context {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> //maybe some additional information<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">the reason for this proposal is for the importance of async models in modern application architectures, and avoid such pyramids:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175);" class="">doSomeStaffInBackground<span style="" class=""> { (resultX) -> </span>Void<span style="" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">doSomeOtherStaff</span> { (resultY) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">if</span> resultY.<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">someCondition</span>(resultX) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">evenMakesItMadness</span>(resultY) { (final) -> <span style="color: rgb(52, 149, 175);" class="">Void</span> <span style="color: rgb(4, 51, 255);" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #008f00" class="">//finally</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">For more context comparing this async syntax to the ‘do try catch’:</div><div class=""><br class=""></div><div class="">do try catch:</div><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: #0433ff" class="">enum</span> Result<T> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> Success(T)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> Failure(ErrorType)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">func</span> flatMap<T,U> (result:<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Result</span><<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span>>, f:<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span>-><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Result</span><<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Result</span><<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</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: #0433ff" class="">switch</span> result{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> .Success(<span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> v): <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">return</span> f(v)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> .Failure(<span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> e): <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">return</span> .<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Failure</span>(e)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">func</span> wrap1<T,U>(f:<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">throws</span>-> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span>-><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Result</span><<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</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: #0433ff" class="">return</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: #0433ff" class="">do</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: #0433ff" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">try</span> .<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Success</span>(f($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: #0433ff" class="">catch</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> e{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">return</span> .<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Failure</span>(e)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">func</span> wrap2<T,U>(f:<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span> -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Result</span><<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">throws</span>-> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(4, 51, 255);" class=""><span style="" class=""> </span>return<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: #0433ff" class="">switch</span> f($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: #0433ff" class="">case</span> .Success(<span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> v): <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">return</span> v</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> .Failure(<span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> e): <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">throw</span> e</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">async:</div><div class=""><br class=""></div><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: #0433ff" class="">func</span> flatMap<T,U> (async: (<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span> -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>, f:<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span> -> (<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>-><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>) -> (<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span> -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</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: #0433ff" class="">return</span>{ cont <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> async{ f($0)(cont) }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><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: #0433ff" class="">func</span> wrap1<T,U>(f:<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span> <span style="color: rgb(4, 51, 255);" class="">future</span>-> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span>->(<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>-><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>)-><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</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: #0433ff" class="">return</span> {input <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> {cont <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">in</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: #0433ff" class="">do</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> cont(<span style="color: rgb(4, 51, 255);" class="">perform</span> f(input))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }<span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">catch</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">_</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">func</span> wrap2<T,U>(f:<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T</span> -> (<span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>-><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>)-><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Void</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">T </span><span style="color: rgb(4, 51, 255);" class="">future</span>-> <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">U</span>{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(4, 51, 255);" class=""><span style="" class=""> </span>return<span style="" class="">{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> f(input)({</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(4, 51, 255);" class="">fulfil</span> $0</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> })</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Z0lfE0AvBRKWSDAcltP5-2FwA6tH7CtZqjBw6KQdxzh8UeEAuMESPncyStoaIO7wH-2B2vOnrjpT7DfQiJxU24qUQ8U-2BXhidcjlyPO3RE5C-2FYQsQvpZZOR7UeJU1UE8cmeX9AUDnr-2BrroPaP-2BrHnPJpHGclAHb8eVYBom-2BPxm3opdU5yc8F-2FKxuD3f-2BlXFkUOqaSgoCPWmSjFfMU1LzSBwPFD-2F0F2UQ-2BXy1vEDkLIzs9lzM-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></div></body></html>