<div dir="ltr"><p style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px;margin-top:0px!important">This is some supplementary explanations about <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> as returning a <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>.</p><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><li>What's <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>?</li><li>Why we need both <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> and <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code></li><li><code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value></code> vs <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value, Error></code></li><li>When forgets <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">try</code></li><li>With side effects</li><li>Why not <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Either</code>, union types nor tuples</li><li>Return value of <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">do</code></li><li>Complication with <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">rethrows</code></li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'"><a id="user-content-whats-result" class="" href="https://gist.github.com/koher/e6a8b128bd7ad6898ac9#whats-result" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent"></a>What's <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>?</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">It's an <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">enum</code> declared in a following way.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">enum</span> Result<span class="" style="color:rgb(167,29,93)"><</span>Value<span class="" style="color:rgb(167,29,93)">></span> {
<span class="" style="color:rgb(167,29,93)">case</span> Success(Value)
<span class="" style="color:rgb(167,29,93)">case</span> Failure(<span class="" style="color:rgb(0,134,179)">ErrorType</span>)
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">It also should have <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">map</code>, <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">flatMap</code> and some convenient methods like <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Optional</code>.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'"><a id="user-content-why-we-need-both-throws-and-result" class="" href="https://gist.github.com/koher/e6a8b128bd7ad6898ac9#why-we-need-both-throws-and-result" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent"></a>Why we need both <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> and <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code></h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code> provides more flexible way to handle errors than <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> though they provide similar functionalities. It can be assigned to a variable, passed to a function and stored in a property while an error must be handled immediately after it is thrown. It is useful especially for asynchronous operations.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">For example, think about <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">map</code> or <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">flatMap</code> (or <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">then</code>) method of <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Promise<Value></code> (or <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Future<Value></code>). They cannot receive a function with <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code>.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">extension</span> Promise {
<span class="" style="color:rgb(167,29,93)">func</span> <span class="" style="color:rgb(121,93,163)">map</span><T>(transform: Value <span class="" style="color:rgb(167,29,93)">-></span> T) <span class="" style="color:rgb(167,29,93)">-></span> Promise<T> { <span class="" style="color:rgb(167,29,93)">...</span> }
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">Because the <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">transform</code> is executed asynchronously, this <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">map</code> method cannot throw an error immediately. If we had <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws </code>as returning a <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>, we can pass a function with <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> to the <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">map</code>.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">func</span> <span class="" style="color:rgb(121,93,163)">toInt</span>(x: <span class="" style="color:rgb(0,134,179)">String</span>) throws <span class="" style="color:rgb(167,29,93)">-></span> <span class="" style="color:rgb(0,134,179)">Int</span> { <span class="" style="color:rgb(167,29,93)">...</span> } <span class="" style="color:rgb(150,152,150)">// -> Result<Int></span>
<span class="" style="color:rgb(167,29,93)">let</span> string: Promise<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">String</span><span class="" style="color:rgb(167,29,93)">></span> <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(167,29,93)">...</span>
<span class="" style="color:rgb(167,29,93)">let</span> number: Promise<span class="" style="color:rgb(167,29,93)"><</span>Result<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">>></span> <span class="" style="color:rgb(167,29,93)">=</span> string<span class="" style="color:rgb(167,29,93)">.</span><span class="" style="color:rgb(0,134,179)">map</span>(toInt)</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">It also caused a problem when I implemented lazily evaluated <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">List<Element></code>s.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">extension</span> List {
<span class="" style="color:rgb(167,29,93)">func</span> <span class="" style="color:rgb(121,93,163)">map</span><T>(transform: Element <span class="" style="color:rgb(167,29,93)">-></span> T) <span class="" style="color:rgb(167,29,93)">-></span> List<T> { <span class="" style="color:rgb(167,29,93)">...</span> }
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">It cannot throw an error because the <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">transform</code> is evaluated lazily. With <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> as returning a <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>, it could be used with a function with <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> too.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">func</span> <span class="" style="color:rgb(121,93,163)">toInt</span>(x: <span class="" style="color:rgb(0,134,179)">String</span>) throws <span class="" style="color:rgb(167,29,93)">-></span> <span class="" style="color:rgb(0,134,179)">Int</span> { <span class="" style="color:rgb(167,29,93)">...</span> } <span class="" style="color:rgb(150,152,150)">// -> Result<Int></span>
<span class="" style="color:rgb(167,29,93)">let</span> strings: List<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">String</span><span class="" style="color:rgb(167,29,93)">></span> <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(167,29,93)">...</span> <span class="" style="color:rgb(150,152,150)">// Infinite list</span>
<span class="" style="color:rgb(167,29,93)">let</span> numbers: List<span class="" style="color:rgb(167,29,93)"><</span>Result<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">>></span> <span class="" style="color:rgb(167,29,93)">=</span> strings<span class="" style="color:rgb(167,29,93)">.</span><span class="" style="color:rgb(0,134,179)">map</span>(toInt)
<span class="" style="color:rgb(167,29,93)">let</span> first10: List<span class="" style="color:rgb(167,29,93)"><</span>Result<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">>></span> <span class="" style="color:rgb(167,29,93)">=</span> numbers<span class="" style="color:rgb(167,29,93)">.</span>take(<span class="" style="color:rgb(0,134,179)">10</span>)
<span class="" style="color:rgb(167,29,93)">let</span> result: Result<span class="" style="color:rgb(167,29,93)"><</span>List<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">>></span> <span class="" style="color:rgb(167,29,93)">=</span> sequence(first10) <span class="" style="color:rgb(150,152,150)">// List<Result<...>> -> Result<List<...>></span>
<span class="" style="color:rgb(167,29,93)">do</span> {
<span class="" style="color:rgb(167,29,93)">let</span> mapped: List<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">></span> <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(167,29,93)">try</span> result
<span class="" style="color:rgb(167,29,93)">...</span>
} <span class="" style="color:rgb(167,29,93)">catch</span> <span class="" style="color:rgb(167,29,93)">let</span> error {
<span class="" style="color:rgb(167,29,93)">...</span>
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">If <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code> is more flexible than <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code>, why do we need <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code>? Handling <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>s manually with <em>manual propagation </em>costs more. We should have a way to handle errors with <em>automatic propagation</em>.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">So we need both <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> and <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'"><a id="user-content-resultvalue-vs-resultvalue-error" class="" href="https://gist.github.com/koher/e6a8b128bd7ad6898ac9#resultvalue-vs-resultvalue-error" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent"></a><code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value></code> vs <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value, Error></code></h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">I know it is discussed which of <em>untyped throws</em> and <em>typed throws</em> are better. If <em>typed throws</em> is accepted, <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value, Error></code> should be provided instead of <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value></code></p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">However <a href="https://github.com/apple/swift-evolution/pull/68" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent">the proposal about <em>typed throws</em></a> has been left for several months. I'm not sure if the discussion is continued. So I started this thread with <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value></code>. And even if <em>typed throws</em> is accepted, we just need to change <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value></code> to <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value, Error></code>. The discussion for <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> as returning a <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code> can be applied for <em>typed throws</em> and <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Value, Error></code> as it is.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'"><a id="user-content-when-forgets-try" class="" href="https://gist.github.com/koher/e6a8b128bd7ad6898ac9#when-forgets-try" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent"></a>When forgets <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">try</code></h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">If we forget to write <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">try</code>, what will happen? This is a downside of <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> as returning a <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">Even if we forget to write <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">try</code>, it can raise an compilation error with <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> as returning a <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>. However the error sites are confusing and nonintuitive.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">func</span> <span class="" style="color:rgb(121,93,163)">toInt</span>(x: <span class="" style="color:rgb(0,134,179)">String</span>) throws <span class="" style="color:rgb(167,29,93)">-></span> <span class="" style="color:rgb(0,134,179)">Int</span> { <span class="" style="color:rgb(167,29,93)">...</span> } <span class="" style="color:rgb(150,152,150)">// -> Result<Int></span>
<span class="" style="color:rgb(167,29,93)">let</span> a <span class="" style="color:rgb(167,29,93)">=</span> toInt(aString) <span class="" style="color:rgb(150,152,150)">// Compilation error here with Swift 2.X</span>
<span class="" style="color:rgb(167,29,93)">let</span> b <span class="" style="color:rgb(167,29,93)">=</span> toInt(bString)
<span class="" style="color:rgb(167,29,93)">let</span> sum <span class="" style="color:rgb(167,29,93)">=</span> a <span class="" style="color:rgb(167,29,93)">+</span> b <span class="" style="color:rgb(150,152,150)">// Compilation error here with `throws` as returning a `Result`</span></pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">I think it can be eased by improved error messages.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'"><a id="user-content-with-side-effects" class="" href="https://gist.github.com/koher/e6a8b128bd7ad6898ac9#with-side-effects" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent"></a>With side effects</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">If a function has side effects, its error should not be ignored implicitly.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">func</span> <span class="" style="color:rgb(121,93,163)">update</span>(x: <span class="" style="color:rgb(0,134,179)">Int</span>) throws { <span class="" style="color:rgb(167,29,93)">...</span> } <span class="" style="color:rgb(150,152,150)">// -> Result<()></span>
update(<span class="" style="color:rgb(0,134,179)">42</span>) <span class="" style="color:rgb(150,152,150)">// No compilation error => dangerous!!</span></pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">So I think <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> should add the <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">@warn_unused_result</code> attribute to the function automatically. If we had a kind of<code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">@error_unused_result</code> attribute, it would be better.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)">update(<span class="" style="color:rgb(0,134,179)">42</span>) <span class="" style="color:rgb(150,152,150)">// Warning or Error</span>
_ <span class="" style="color:rgb(167,29,93)">=</span> update(<span class="" style="color:rgb(0,134,179)">42</span>) <span class="" style="color:rgb(150,152,150)">// Ignores error explicitly</span>
<span class="" style="color:rgb(150,152,150)">// Manual propagation</span>
<span class="" style="color:rgb(167,29,93)">switch</span> update(<span class="" style="color:rgb(0,134,179)">42</span>) {
<span class="" style="color:rgb(167,29,93)">case</span> <span class="" style="color:rgb(167,29,93)">.</span>Success:
<span class="" style="color:rgb(167,29,93)">...</span>
<span class="" style="color:rgb(167,29,93)">case</span> <span class="" style="color:rgb(167,29,93)">.</span>Failure(error):
<span class="" style="color:rgb(167,29,93)">...</span>
}
<span class="" style="color:rgb(150,152,150)">// Automatic propagation</span>
<span class="" style="color:rgb(167,29,93)">do</span> {
<span class="" style="color:rgb(167,29,93)">try</span> update(<span class="" style="color:rgb(0,134,179)">42</span>)
<span class="" style="color:rgb(167,29,93)">...</span>
} <span class="" style="color:rgb(167,29,93)">catch</span> <span class="" style="color:rgb(167,29,93)">let</span> error {
<span class="" style="color:rgb(167,29,93)">...</span>
}</pre></div><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'"><a id="user-content-why-not-either-union-types-nor-tuples" class="" href="https://gist.github.com/koher/e6a8b128bd7ad6898ac9#why-not-either-union-types-nor-tuples" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent"></a>Why not <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Either</code>, union types nor tuples</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code> is preferred to <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Either</code> as discussed on <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/007728.html" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent">this thread</a>.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Either</code> is a tagged union. However its tags are meaningless: <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Left</code> and <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Right</code>. I think it is much better to have something like <em>union types</em> in Ceylon and some other languages than to have <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Either</code>.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">However <em>union types</em> make a significant impact on the type system. Subtyping gets much more complicated. We should also think about <em>intersection types</em> in addition to <em>uniton types</em>. If we had <em>union types</em>, I think it would be better that <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Optional<Foo></code> is a sugar of <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Foo|Nil</code> like in Ceylon. Changing all of them is not practical.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">How about tuples? Tuples like <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">(Value?, Error?)</code> is an easy way. However it results four cases: <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">(value, nil)</code>, <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">(nil, error)</code>, <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">(value, error)</code> and <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">(nil, nil)</code>. We don't need the last two.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">Therefore I think <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code> is the best way.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'"><a id="user-content-return-value-of-do" class="" href="https://gist.github.com/koher/e6a8b128bd7ad6898ac9#return-value-of-do" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent"></a>Return value of <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">do</code></h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">We easily think of a return value of <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">do</code> statement/expression like Haskell's <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">do</code> notation.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">func</span> <span class="" style="color:rgb(121,93,163)">toInt</span>(x: <span class="" style="color:rgb(0,134,179)">String</span>) throws <span class="" style="color:rgb(167,29,93)">-></span> <span class="" style="color:rgb(0,134,179)">Int</span> { <span class="" style="color:rgb(167,29,93)">...</span> } <span class="" style="color:rgb(150,152,150)">// -> Result<Int></span>
<span class="" style="color:rgb(167,29,93)">let</span> sum: Result<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">></span> <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(167,29,93)">do</span> {
<span class="" style="color:rgb(167,29,93)">let</span> a: <span class="" style="color:rgb(0,134,179)">Int</span> <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(167,29,93)">try</span> toInt(<span class="" style="color:rgb(24,54,145)"><span class="">"</span>2<span class="">"</span></span>)
<span class="" style="color:rgb(167,29,93)">let</span> b: <span class="" style="color:rgb(0,134,179)">Int</span> <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(167,29,93)">try</span> toInt(<span class="" style="color:rgb(24,54,145)"><span class="">"</span>3<span class="">"</span></span>)
a <span class="" style="color:rgb(167,29,93)">+</span> b
} <span class="" style="color:rgb(150,152,150)">// Result(5)</span></pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">It can be regarded as a syntactic sugar of nested <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">flatMap</code>s.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">However it causes following problems.</p><ol style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><li>It made it impossible to <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">return</code>, <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">break</code> nor <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">continue</code> inside the <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">do</code> statement/expression.</li><li>Returning the evaluated value of the last expression in braces is not Swifty.</li></ol><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">I think the following can be the alternative.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">let</span> sum: Result<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">></span> <span class="" style="color:rgb(167,29,93)">=</span> { () <span class="" style="color:rgb(167,29,93)">throws</span> <span class="" style="color:rgb(167,29,93)">-></span> <span class="" style="color:rgb(0,134,179)">Int</span> <span class="" style="color:rgb(167,29,93)">in</span>
<span class="" style="color:rgb(167,29,93)">let</span> a: <span class="" style="color:rgb(0,134,179)">Int</span> <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(167,29,93)">try</span> toInt(<span class="" style="color:rgb(24,54,145)"><span class="">"</span>2<span class="">"</span></span>)
<span class="" style="color:rgb(167,29,93)">let</span> b: <span class="" style="color:rgb(0,134,179)">Int</span> <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(167,29,93)">try</span> toInt(<span class="" style="color:rgb(24,54,145)"><span class="">"</span>3<span class="">"</span></span>)
<span class="" style="color:rgb(167,29,93)">return</span> a <span class="" style="color:rgb(167,29,93)">+</span> b
}()</pre></div><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'"><a id="user-content-complication-with-rethrows" class="" href="https://gist.github.com/koher/e6a8b128bd7ad6898ac9#complication-with-rethrows" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent"></a>Complication with <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">rethrows</code></h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">With <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">throws</code> as returning a <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result</code>, what should be the type of the following <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">numbers</code>?</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">func</span> <span class="" style="color:rgb(121,93,163)">toInt</span>(x: <span class="" style="color:rgb(0,134,179)">String</span>) throws <span class="" style="color:rgb(167,29,93)">-></span> <span class="" style="color:rgb(0,134,179)">Int</span> { <span class="" style="color:rgb(167,29,93)">...</span> } <span class="" style="color:rgb(150,152,150)">// -> Result<Int></span>
<span class="" style="color:rgb(167,29,93)">let</span> numbers: ??? <span class="" style="color:rgb(167,29,93)">=</span> [<span class="" style="color:rgb(24,54,145)"><span class="">"</span>one<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>2<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>3<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>four<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>5<span class="">"</span></span>]<span class="" style="color:rgb(167,29,93)">.</span><span class="" style="color:rgb(0,134,179)">map</span>(toInt)</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">It can be regarded as both <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<List<Int>></code> and <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">List<Result<Int>></code>. I think it should be decided by the type of the <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">numbers</code>.</p><div class="" style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;background-color:rgb(247,247,247)"><span class="" style="color:rgb(150,152,150)">// Both works</span>
<span class="" style="color:rgb(167,29,93)">let</span> numbers: Result<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Array</span><span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">>></span> <span class="" style="color:rgb(167,29,93)">=</span> [<span class="" style="color:rgb(24,54,145)"><span class="">"</span>one<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>2<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>3<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>four<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>5<span class="">"</span></span>]<span class="" style="color:rgb(167,29,93)">.</span><span class="" style="color:rgb(0,134,179)">map</span>(toInt)
<span class="" style="color:rgb(167,29,93)">let</span> numbers: <span class="" style="color:rgb(0,134,179)">Array</span><span class="" style="color:rgb(167,29,93)"><</span>Result<span class="" style="color:rgb(167,29,93)"><</span><span class="" style="color:rgb(0,134,179)">Int</span><span class="" style="color:rgb(167,29,93)">>></span> <span class="" style="color:rgb(167,29,93)">=</span> [<span class="" style="color:rgb(24,54,145)"><span class="">"</span>one<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>2<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>3<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>four<span class="">"</span></span>, <span class="" style="color:rgb(24,54,145)"><span class="">"</span>5<span class="">"</span></span>]<span class="" style="color:rgb(167,29,93)">.</span><span class="" style="color:rgb(0,134,179)">map</span>(toInt)</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px">If the type of the <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">numbers</code> are omitted, what happens? We have some options.</p><ul style="padding-left:2em;margin-top:0px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;line-height:25.6px;margin-bottom:0px!important"><li>Compilation error because of the ambiguous type</li><li>The default type like that the one for integer literals is <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Int</code></li><ul style="padding-left:2em;margin-top:0px;margin-bottom:0px"><li>e.g. If <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">map</code> is marked as <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">rethrows</code>, returns <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Result<Array<Int>></code>, and <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Array<Result<Int>></code> for the others.</li></ul></ul><div><font color="#333333" face="Helvetica Neue, Helvetica, Segoe UI, Arial, freesans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol"><span style="font-size:16px;line-height:25.6px"><br></span></font></div><div><font color="#333333" face="Helvetica Neue, Helvetica, Segoe UI, Arial, freesans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol"><span style="font-size:16px;line-height:25.6px">-- Yuta</span></font></div></div>