<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Oops, forgot to put the initializers that are the most useful:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">init</span>(<span style="color: #ba2da2" class="">_</span> expression: <span style="color: #ba2da2" class="">@autoclosure</span> () <span style="color: #ba2da2" class="">throws</span> -> <span style="color: #4f8187" class="">T</span>) {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">do</span> { <span style="color: #ba2da2" class="">self</span> = .<span style="color: #31595d" class="">value</span>(<span style="color: #ba2da2" class="">try</span> expression() ) }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">catch</span> { <span style="color: #ba2da2" class="">self</span> = .<span style="color: #31595d" class="">error</span>(error) }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">init</span>(<span style="color: #ba2da2" class="">_</span> closure: () <span style="color: #ba2da2" class="">throws</span> -> <span style="color: #4f8187" class="">T</span>) {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">do</span> { <span style="color: #ba2da2" class="">self</span> = .<span style="color: #31595d" class="">value</span>(<span style="color: #ba2da2" class="">try</span> closure()) }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">catch</span> { <span style="color: #ba2da2" class="">self</span> = .<span style="color: #31595d" class="">error</span>(error) }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div class=""><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class="">On Nov 14, 2017, at 6:57 PM, Hooman Mehr 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; line-break: after-white-space;" class="">You can support all styles with enum as well. What don’t you do this:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">enum</span> Result<T> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> value(<span style="color: #4f8187" class="">T</span>)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> error(<span style="color: #703daa" class="">Error</span>)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">init</span>(<span style="color: #ba2da2" class="">_</span> value: <span style="color: #4f8187" class="">T</span>) { <span style="color: #ba2da2" class="">self</span> = .<span style="color: #31595d" class="">value</span>(value) }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">init</span>(error: <span style="color: #703daa" class="">Error</span>) { <span style="color: #ba2da2" class="">self</span> = .<span style="color: #31595d" class="">error</span>(error) }</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">func</span> get() <span style="color: #ba2da2" class="">throws</span> -> <span style="color: #4f8187" class="">T</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">switch</span> <span style="color: #ba2da2" class="">self</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .<span style="color: #31595d" class="">value</span>(value): <span style="color: #ba2da2" class="">return</span> value</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .<span style="color: #31595d" class="">error</span>(error): <span style="color: #ba2da2" class="">throw</span> error }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">func</span> map<U>(<span style="color: #ba2da2" class="">_</span> transform: (<span style="color: #4f8187" class="">T</span>) <span style="color: #ba2da2" class="">throws</span> -> <span style="color: #4f8187" class="">U</span>) <span style="color: #ba2da2" class="">throws</span> -> <span style="color: #4f8187" class="">U</span> { <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">try</span> transform(<span style="color: #31595d" class="">get</span>()) }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">var</span> value: <span style="color: #4f8187" class="">T</span>? {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">switch</span> <span style="color: #ba2da2" class="">self</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .<span style="color: #31595d" class="">value</span>(value): <span style="color: #ba2da2" class="">return</span> value</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> .<span style="color: #31595d" class="">error</span>: <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">nil</span> }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">public</span> <span style="color: #ba2da2" class="">var</span> error: <span style="color: #703daa" class="">Error</span>? {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">switch</span> <span style="color: #ba2da2" class="">self</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> .<span style="color: #31595d" class="">value</span>: <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> <span style="color: #ba2da2" class="">let</span> .<span style="color: #31595d" class="">error</span>(error): <span style="color: #ba2da2" class="">return</span> error }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Nov 14, 2017, at 3:40 PM, Guillaume Lessard 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="">I’m late to this particular party, but having just caught up with the thread I’m surprised that no one substantially addressed the stylistic duality that a `public enum Result` would bring.<br class=""><br class="">In short, I’d rather Result be a struct than an enum.<br class=""><br class="">I too implemented a Result, for obvious reasons. I was, however, unsatisfied that it added another interface for error handling, namely switching over the enum — it’s not really better, not really worse, but now there are more error handling patterns to look for in your code.<br class=""><br class="">My solution was to simply switch to a `struct Result`, where the enum is private. The only way to get the value out is via a throwing method. See <<a href="https://gist.github.com/glessard/48f4c1305ac20b1b99c1bbdc2fb6290c" class="">https://gist.github.com/glessard/48f4c1305ac20b1b99c1bbdc2fb6290c</a>> for a no-frills implementation.<br class=""><br class="">This has all the advantages of the Result enum — it’s easily used in asynchronous situations and can implement the desired functional/monadic niceties, but without exposing an unnecessary alternate interface to Swift’s native error handling.<br class=""><br class="">Cheers,<br class="">Guillaume Lessard<br class=""><br class="">_______________________________________________<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></div></blockquote></div><br class=""></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></body></html>