<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=""><div class="">Nice summary!</div><div class=""><br class=""></div><div class="">The first "problem" can be solved even easier!!! Due to a feature of Swift I just encountered:</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: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var</span> num1: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>? = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>num1? = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(num1) <span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// still nil</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>num1 = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(num1) </span>// Optional(2)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>num1? = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(num1) </span>// Optional(3)</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: rgb(0, 0, 0);" class="">num1? = </span><span style="color: rgb(187, 44, 162);" class="">nil</span><span style="color: rgb(0, 0, 0);" class=""> </span>// not allowed</div><div class=""><br class=""></div><div class="">If you put a "?" after the optional variable it only changes the variable if the variable is not nil. Assignments to "nil" are prohibited if "?" is used. So it is like a nil check and assignments happen only at the unwrapped type level ("Int" not "Int?"):</div><div class=""><br class=""></div><div class="">For "double Optionals":</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: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var</span> num2: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>?? = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>num2? = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(num2) </span>// still Optional(nil)</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>num2 = .<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">Some</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>num2? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// assigns; only one "unwrap"</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(num2) </span>// Optional(Optional(5))</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>num2 = .<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">Some</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>num2?? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">6</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// "double unwrap"</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(num2) </span>// still Optional(nil)</div></div><div class=""><br class=""></div>In our case we can "solve" the first problem the following way:<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: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var</span> numbers: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>] = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>numbers<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span>? = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span> <span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// not allowed</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> array: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>?] = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span>? = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil </span><span style="color: rgb(0, 132, 0);" class="">// assignment is guaranteed to succeed if index exists</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(array) </span>// [nil]</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Having subscripts which throw are good for catching "missing" indices. Although they require you to write "try" everywhere you use it and checking if the assignment comes through can also be done by unwrapping and optional assignment nil checks:</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: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> value = array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span> {}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span>// vs</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">do</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> value = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">try</span> array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span> } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">catch</span> {}</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> (array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span>? = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span>) != <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span> {}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span>// vs</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">do</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">try</span> array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span> } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">catch</span> {}</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> value = array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> (array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span>? = value) != <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span> {}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span>// vs</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">do</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">try</span> array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">try</span> array<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span> } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">catch</span> {}</div></div><div class=""><br class=""></div><div class="">Although I’m generally +1 for adding throwing subscripts but using them in this case doesn’t seem to be that useful.</div><div class=""><br class=""></div><div class="">- Maximilian</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">Am 07.02.2016 um 00:19 schrieb <a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</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="">Ah, ok, I think I understand your objection now. Let me try to summarize…</div><div class=""><br class=""></div><div class="">If we extend on MutableCollectionType like this:</div><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><span style="color: rgb(211, 54, 130);" class="">subscript</span>(ifExists idx: <span style="color: rgb(39, 139, 210);" class="">Self</span>.<span style="color: rgb(39, 139, 210);" class="">Index</span>) -> <span style="color: rgb(39, 139, 210);" class="">Self</span>.<span style="color: rgb(39, 139, 210);" class="">Generator</span>.<span style="color: rgb(39, 139, 210);" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="color: rgb(211, 54, 130);" class="">get</span> { <span style="color: rgb(211, 54, 130);" class="">return</span> <span style="color: rgb(211, 54, 130);" class="">self</span>.<span style="color: rgb(39, 139, 210);" class="">indices</span>.<span style="color: rgb(39, 139, 210);" class="">contains</span>(idx) ? <span style="color: rgb(211, 54, 130);" class="">self</span>[idx] : <span style="color: rgb(211, 54, 130);" class="">nil</span> <span style="color: rgb(211, 54, 130);" class="">as</span> <span style="color: rgb(39, 139, 210);" class="">Self</span>.<span style="color: rgb(39, 139, 210);" class="">Generator</span>.<span style="color: rgb(39, 139, 210);" class="">Element</span>? }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="color: rgb(211, 54, 130);" class="">set</span> { <span style="color: rgb(211, 54, 130);" class="">if</span> <span style="color: rgb(211, 54, 130);" class="">let</span> nv = newValue <span style="color: rgb(211, 54, 130);" class="">where</span> <span style="color: rgb(211, 54, 130);" class="">self</span>.<span style="color: rgb(39, 139, 210);" class="">indices</span>.<span style="color: rgb(39, 139, 210);" class="">contains</span>(idx) { <span style="color: rgb(211, 54, 130);" class="">self</span>[idx] = nv } }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(203, 75, 22);" class=""><span style="color: rgb(147, 161, 161);" class="">}</span></div></div><div class="">then, when <span style="font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class="">CollectionType.Generator</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class="">.</span><span style="font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class="">Element</span> is <span style="color: rgb(39, 139, 210); font-family: 'Fira Mono';" class="">NilLiteralConvertible</span>, we have the following problem:</div><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><span style="color: rgb(211, 54, 130);" class="">var</span> array: [<span style="color: rgb(39, 139, 210);" class="">Int</span>?] = [<span style="color: rgb(108, 113, 196);" class="">1</span>]</div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">array[</span><font color="#93a1a1" face="Fira Mono" class="">ifExists: </font><span style="color: rgb(108, 113, 196); font-family: 'Fira Mono';" class="">0</span><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">]</span><font color="#93a1a1" face="Fira Mono" class=""> = </font><span style="color: rgb(211, 54, 130); font-family: 'Fira Mono';" class="">nil</span><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class=""> </span><font color="#cb4b16" face="FiraMono-Regular" class="">// *PROBLEM*: The most straight-forward way of safely setting something to nil silently fails because the compiler treats this as an Int?? instead of an Int?</font></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">array</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// "[Optional(1)]"</div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">array[</span><font color="#93a1a1" face="Fira Mono" class="">ifExists: </font><span style="color: rgb(108, 113, 196); font-family: 'Fira Mono';" class="">0</span><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">]</span><font color="#93a1a1" face="Fira Mono" class=""> = </font><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">array[</span><font color="#93a1a1" face="Fira Mono" class="">ifExists: </font><span style="color: rgb(108, 113, 196); font-family: 'Fira Mono';" class="">1</span><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">] </span><span style="color: rgb(203, 75, 22); font-family: FiraMono-Regular;" class="">// *No problem*</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">array</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// "[Optional(1)]"</div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><span style="color: rgb(41, 161, 152);" class="">array[</span><font color="#93a1a1" class="">ifExists: </font><span style="color: rgb(108, 113, 196);" class="">0</span><span style="color: rgb(41, 161, 152);" class="">]</span><font color="#93a1a1" class=""> = </font><span style="color: rgb(211, 54, 130);" class="">nil</span> <span style="color: rgb(211, 54, 130);" class="">as</span> <span style="color: rgb(39, 139, 210);" class="">Int</span><font color="#93a1a1" class="">? </font></font><span style="color: rgb(203, 75, 22); font-family: FiraMono-Regular;" class="">// *No problem*</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">array</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// "[nil]"</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161); min-height: 14px;" class=""><br class=""></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class="">But if we fix it to allow an unannotated <span style="color: rgb(211, 54, 130); font-family: 'Fira Mono';" class="">nil</span> to go through:</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(ifExists idx: </span>Self<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">.</span>Index<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) -> </span>Self<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">? {</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">get</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.indices.contains(idx) ? <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] : <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">as</span> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Element</span>? }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(203, 75, 22);" class=""><span style="color: rgb(147, 161, 161);" class=""> </span><span style="color: rgb(211, 54, 130);" class="">set</span><span style="color: rgb(147, 161, 161);" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.indices.contains(idx) {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span> nv = newValue {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] = nv</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> } <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">else</span> {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span> nilType = <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">as</span>? <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">NilLiteralConvertible</span> {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] = (nilType.<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">dynamicType</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">init</span>(nilLiteral: ()) <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">as</span>! <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Element</span>)</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">}</div><div class="">then we have a different problem:</div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(211, 54, 130);" class="">var</span> array: [<span style="color: rgb(39, 139, 210);" class="">Int</span>?] = [<span style="color: rgb(108, 113, 196);" class="">1, 2, 3</span>]</div></div><div class=""><div style="color: rgb(147, 161, 161); font-family: 'Fira Mono'; margin: 0px; line-height: normal;" class=""><span style="color: rgb(41, 161, 152);" class="">array[</span>ifExists: <span style="color: rgb(108, 113, 196);" class="">0</span><span style="color: rgb(41, 161, 152);" class="">]</span> = <span style="color: rgb(211, 54, 130);" class="">nil </span><span style="color: rgb(203, 75, 22); font-family: FiraMono-Regular;" class="">// *No problem*: The code no longer cares that this is an Int?? instead of an Int?</span></div></div><div class=""><div style="color: rgb(203, 75, 22); font-family: 'Fira Mono'; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">array</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// "[nil, Optional(2), Optional(3)]"</div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">array[</span><font color="#93a1a1" face="Fira Mono" class="">ifExists: </font><span style="color: rgb(108, 113, 196); font-family: 'Fira Mono';" class="">1</span><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">]</span><font color="#93a1a1" face="Fira Mono" class=""> = </font><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">array[</span><font color="#93a1a1" face="Fira Mono" class="">ifExists: </font><span style="color: rgb(108, 113, 196); font-family: 'Fira Mono';" class="">3</span><span style="color: rgb(41, 161, 152); font-family: 'Fira Mono';" class="">]</span><font color="#cb4b16" style="color: rgb(147, 161, 161); font-family: FiraMono-Regular;" class=""> </font><font color="#cb4b16" face="FiraMono-Regular" class="">// *PROBLEM*: This shouldn’t do anything, but it succeeds because the code no longer cares that it’s an Int?? instead of an Int?</font></div><div style="color: rgb(203, 75, 22); font-family: 'Fira Mono'; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">array</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// "[nil, nil, Optional(3)]"</div><div style="color: rgb(147, 161, 161); font-family: 'Fira Mono'; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">array[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">2</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">]</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">as</span> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>?<font face="FiraMono-Regular" style="" class=""><font color="#93a1a1" class=""> </font></font><span style="color: rgb(203, 75, 22); font-family: FiraMono-Regular;" class="">// *No problem*</span></div><div style="color: rgb(203, 75, 22); font-family: 'Fira Mono'; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">array</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// "[nil, nil, nil]"</div><div style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class=""><br class=""></div></div></div></div><div class="">Assuming we’re all on the same page now… Yeah, as much as I’d love to keep the <span style="font-family: 'Fira Mono'; color: rgb(41, 161, 152);" class="">array[</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class="">ifExists: </span><span style="font-family: 'Fira Mono'; color: rgb(108, 113, 196);" class="">0</span><span style="font-family: 'Fira Mono'; color: rgb(41, 161, 152);" class="">]</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class=""> = </span><span style="font-family: 'Fira Mono'; color: rgb(211, 54, 130);" class="">nil</span> behavior from the “fixed” version, I agree that the 2nd problem is clearly worse than the 1st, especially since being explicit about your nils (<span style="font-family: 'Fira Mono'; color: rgb(41, 161, 152);" class="">array[</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class="">ifExists: </span><span style="font-family: 'Fira Mono';" class=""><font color="#6c71c4" class="">0</font></span><span style="font-family: 'Fira Mono'; color: rgb(41, 161, 152);" class="">]</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class=""> = </span><span style="font-family: 'Fira Mono'; color: rgb(211, 54, 130);" class="">nil</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class=""> </span><span style="font-family: 'Fira Mono'; color: rgb(211, 54, 130);" class="">as</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class=""> </span><span style="font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class="">Int</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class="">?</span>) allows the assignment to go through.</div><div class=""><br class=""></div><div class="">So I’m in favor of the 1st one, which doesn’t allow unannotated nil assignments to succeed.</div><div class=""><br class=""></div><div class="">The only alternative I can think of is changing the language to allow subscripts to throw errors:</div><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">enum</span> IndexError : <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">ErrorType</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> outOfRange }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">subscript</span>(throwing idx: <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">Self</span>.Index) <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">throws</span> -> <span style="color: rgb(39, 139, 210);" class="">Self</span>.<span style="color: rgb(39, 139, 210);" class="">Generator</span>.<span style="color: rgb(39, 139, 210);" class="">Element</span> {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="color: rgb(211, 54, 130);" class="">get</span> {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">guard</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.<span style="color: rgb(39, 139, 210);" class="">indices</span>.<span style="color: rgb(39, 139, 210);" class="">contains</span>(idx) <span style="color: rgb(211, 54, 130);" class="">else</span> { <span style="color: rgb(211, 54, 130);" class="">throw</span> <span style="color: rgb(41, 161, 152);" class="">IndexError</span>.<span style="color: rgb(41, 161, 152);" class="">outOfRange</span> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx]</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><font color="#93a1a1" face="Fira Mono" class=""> </font><span style="color: rgb(211, 54, 130);" class="">set</span><font color="#93a1a1" face="Fira Mono" class=""> {</font></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">guard</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.<span style="color: rgb(39, 139, 210);" class="">indices</span>.<span style="color: rgb(39, 139, 210);" class="">contains</span>(idx) <span style="color: rgb(211, 54, 130);" class="">else</span> { <span style="color: rgb(211, 54, 130);" class="">throw</span> <span style="color: rgb(41, 161, 152);" class="">IndexError</span>.<span style="color: rgb(41, 161, 152);" class="">outOfRange</span> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] = newValue</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">}</div></div><div class=""><br class=""></div><div class="">Although… They aren’t mutually exclusive… It’s obviously not hard to imagine scenarios in which you don’t really care that you're out of bounds and just want to avoid crashing (otherwise we wouldn’t have been having this conversation in the first place) but nor is it hard to imagine scenarios in which you might want explicit confirmation that there wasn’t an error, rather than having to check for nil or compare before & after versions of the array. In those cases the throwing version would be appropriate. Plus, if your subscript function doesn’t only pass-through to an underlying collection, it might be handy to be able to throw, say, an OutOfMemory error if your subscript function loads or generates some large data structure. Or perhaps you’re writing a file manager, and you’re trying to get files by “subscripting” directories… The file may exist, but you might not have read permission.</div><div class=""><br class=""></div><div class="">Should we propose both?</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 6, 2016, at 12:52, Maximilian Hünenberger 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 dir="auto" class=""><div class=""></div><div class="">Probably I wasn't clear about that. Sorry.</div><div class=""><br class=""></div><div class="">My concern was about adding (code from Dave Sweeris):</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class="">extension</span> <span class="">Array</span> <span class="">where</span> Element: NilLiteralConvertible {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">subscript</span>(ifExists idx: <span class="">Index</span>) -> <span class="">Element</span>? {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">get</span> { <span class="">return</span> (<span class="">startIndex</span> ..< <span class="">endIndex</span>) ~= idx ? <span class="">self</span>[idx] : <span class="">nil</span> }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">set</span> { <span class="">if</span> (<span class="">startIndex</span> ..< <span class="">endIndex</span>) ~= idx { <span class="">self</span>[idx] = newValue ?? Element(nilLiteral: ())} }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">}</span></div></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">Since it would allow this:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">var array: [Int?] = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">array[ifExists: 0] = nil // sets array[0] to nil if index is valid</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">print(array) // "[nil]"</span></div><div class="" style="margin: 0px; line-height: normal;">array = [1]</div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">array[ifExists: 0] = array[ifExists: 1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">print(array) // "[nil]"</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">Whereas the normal behavior:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">var array: [Int?] = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">array[ifExists: 0] = nil // does nothing</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">print(array) // "[1]"</span></div><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">array[ifExists: 0] = array[ifExists: 1] // does nothing</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">print(array) // "[1]"</span></div></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">Hope this clarifies my point</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">- Maximilian</span></div></div><div class=""><br class="">Am 06.02.2016 um 21:31 schrieb Thorsten Seitz <<a href="mailto:tseitz42@icloud.com" class="">tseitz42@icloud.com</a>>:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div class=""></div><div class=""><br class=""></div><div class=""><br class="">Am 06.02.2016 um 00:58 schrieb Maximilian Hünenberger via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div class=""></div><div class="">You are totally right. The return type is "Int??".</div><div class=""><br class=""></div><div class="">My point was that if we allowed something like this (as suggested by Dave Sweeris I think):</div><div class=""><br class=""></div><div class=""> var array: [Int?] = [1]</div><div class=""> array[ifExists: 0] = nil</div><div class=""><br class=""></div><div class="">To set the element at index 0 to nil instead of doing nothing.</div><div class="">The next example would also set index 0 to nil even though the getter failed:</div><div class=""><br class=""></div><div class=""> <span style="background-color: rgba(255, 255, 255, 0);" class="">array[ifExists: 0] = array[ifExists: 1]</span></div></div></blockquote><div class=""><br class=""></div>No, it doesn't. Just try it out.<div class=""><br class=""></div><div class="">-Thorsten </div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">- Maximilian</div><div class=""><br class="">Am 05.02.2016 um 10:20 schrieb Haravikk <<a href="mailto:swift-evolution@haravikk.me" class="">swift-evolution@haravikk.me</a>>:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 4 Feb 2016, at 20:24, Maximilian Hünenberger via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I just realized that the normal setter for failable lookups is very nice in case of assigning/swapping:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);"><div class="" style="margin: 0px; line-height: normal; color: rgb(211, 54, 130);">extension<span class="" style="color: rgb(147, 161, 161);"> </span><span class="" style="color: rgb(39, 139, 210);">Array</span><span class="" style="color: rgb(147, 161, 161);"> {</span></div><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(211, 54, 130);">subscript</span>(ifExists idx: <span class="" style="color: rgb(39, 139, 210);">Index</span>) -> <span class="" style="color: rgb(39, 139, 210);">Element</span>? {</div><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(211, 54, 130);">get</span> { <span class="" style="color: rgb(211, 54, 130);">return</span> (<span class="" style="color: rgb(39, 139, 210);">startIndex</span> ..< <span class="" style="color: rgb(39, 139, 210);">endIndex</span>) ~= idx ? <span class="" style="color: rgb(211, 54, 130);">self</span>[idx] : <span class="" style="color: rgb(211, 54, 130);">nil</span> }</div><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(211, 54, 130);">set</span> { <span class="" style="color: rgb(211, 54, 130);">if</span> (<span class="" style="color: rgb(39, 139, 210);">startIndex</span> ..< <span class="" style="color: rgb(39, 139, 210);">endIndex</span>) ~= idx && newValue != <span class="" style="color: rgb(211, 54, 130);">nil</span> { <span class="" style="color: rgb(211, 54, 130);">self</span>[idx] = newValue! } }</div><div class="" style="margin: 0px; line-height: normal;"> }</div><div class="" style="margin: 0px; line-height: normal;">}</div></div></div></div></blockquote></div><div class=""><br class=""></div><div class=""> // array[index1] is only set if both indexes are valid</div><div class=""> array[ifExists: index1] = array[ifExists: index2] </div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">if array is of type [Int?] and the special setter for optional Elements would have been added:</div><div class=""><br class=""></div><div class="">array[index1] would be set to "nil" if array[index2] is nil <b style="text-decoration: underline;" class="">or</b> index2 is not valid which is unfortunate.</div></div></div></blockquote></div><br class=""><div class="">Wouldn’t the return type be Int?? in this case? It’s not as pretty to test for as a plain Int? but iirc you can still distinguish a return type of nil from an optional that happens to contain nil, which should allow you to tell the difference between a nil value and an invalid index, I just can’t recall how at the moment (as I design around cases like these like my life depends on it ;)</div></div></blockquote></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></div></blockquote></div>_______________________________________________<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></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></body></html>