<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="">I don't like that the subscript essentially becomes a second call syntax. Can we solve these problems in a different way?<br class=""><div class="">
<br class="Apple-interchange-newline"><span style="color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none;" class="">Félix</span>
</div>

<br class=""><div><blockquote type="cite" class=""><div class="">Le 28 janv. 2016 à 22:20:01, Dave via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</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="">What do we think about allowing types to have static subscripts?</div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="font-size: 11px; margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class=""><span style="color: rgb(211, 54, 130);" class="">struct</span> Foo {</font></div><div style="font-size: 11px; margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">subscript</span>(bar: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Bar</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">FooBar</span> {...}</font></div><div style="font-size: 11px; margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">}</font></div><div class=""><br class=""></div><div class="">It could be used to implement umbrella class “initializers” that return subclasses (or anything, really) without relying on Objective-C tomfoolery, since it’s just another function and can return anything you want:</div><div class=""><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><span style="color: rgb(211, 54, 130); font-size: 11px;" class="">class</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> Int8Class&nbsp; &nbsp; : </span></font><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">SwiftInteger</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> {…}</span></font></font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">class</span> Int16Class &nbsp; : <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">SwiftInteger</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> x: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int16</span>;&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">override</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">init</span>() { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">x</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">0</span> }; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">_</span> x: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int16</span>)&nbsp; { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">x</span> = x } }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><span style="color: rgb(211, 54, 130); font-size: 11px;" class="">class</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> Int32Class &nbsp; : </span></font><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">SwiftInteger</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> {…}</span></font></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><span style="color: rgb(211, 54, 130); font-size: 11px;" class="">class</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> IntClass &nbsp; &nbsp; : </span></font><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">SwiftInteger</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> {…}</span></font></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><span style="color: rgb(211, 54, 130); font-size: 11px;" class="">class</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> BigIntClass&nbsp; : </span></font><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">SwiftInteger</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> {…}</span></font></font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><span style="font-family: FiraMono-Regular; color: rgb(211, 54, 130);" class="">class</span><span style="font-family: FiraMono-Regular;" class=""> SwiftInteger&nbsp;</span><span style="font-family: FiraMono-Regular;" class="">{</span></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><font color="#93a1a1" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span></font><span style="color: rgb(211, 54, 130); font-size: 11px;" class="">static</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class="">&nbsp;</span></font><span style="color: rgb(211, 54, 130); font-size: 11px;" class="">subscript</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class="">()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; </span></font><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">SwiftInteger</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> {…}</span></font></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><font color="#93a1a1" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span></font><span style="color: rgb(211, 54, 130); font-size: 11px;" class="">static</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class="">&nbsp;</span></font><span style="color: rgb(211, 54, 130); font-size: 11px;" class="">subscript</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class="">(x: </span></font><span style="color: rgb(39, 139, 210); font-size: 11px;" class="">Int8</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class="">) &nbsp; -&gt; </span></font><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">SwiftInteger</span><font color="#93a1a1" class=""><span style="font-size: 11px;" class=""> {…}</span></font></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" style="color: rgb(147, 161, 161); font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">static</span>&nbsp;<span style="color: rgb(211, 54, 130);" class="">subscript</span>(x: <span style="color: rgb(39, 139, 210);" class="">Int16</span>)&nbsp; -&gt; <span style="color: rgb(41, 161, 152);" class="">SwiftInteger</span> {</font><font color="#93a1a1" face="FiraMono-Regular" class=""><span style="font-size: 11px;" class="">…}</span></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" style="color: rgb(147, 161, 161); font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">static</span>&nbsp;<span style="color: rgb(211, 54, 130);" class="">subscript</span>(x: <span style="color: rgb(39, 139, 210);" class="">Int32</span>) &nbsp;-&gt; <span style="color: rgb(41, 161, 152);" class="">SwiftInteger</span> {</font><font color="#93a1a1" face="FiraMono-Regular" class=""><span style="font-size: 11px;" class="">…}</span></font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">static</span>&nbsp;<span style="color: rgb(211, 54, 130);" class="">subscript</span>(x: <span style="color: rgb(39, 139, 210);" class="">Int</span>) &nbsp; &nbsp;-&gt; <span style="color: rgb(41, 161, 152);" class="">SwiftInteger</span> {</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">switch</span> x {</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>( <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int8</span>.min)...<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>( <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int8</span>.max): <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">Int8Class</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int8</span>(x))</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int16</span>.min)...<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int16</span>.max): <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">Int16Class</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int16</span>(x))</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int32</span>.min)...<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int32</span>.max): <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">Int32Class</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int32</span>(x))</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">default</span>:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">IntClass</span>(x)</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; }</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(41, 161, 152);" class=""><span style="font-family: FiraMono-Regular; color: rgb(147, 161, 161);" class="">}</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(41, 161, 152);" class=""><font face="FiraMono-Regular" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> foo = </span>SwiftInteger[<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">256</span>]&nbsp;<span style="color: rgb(203, 75, 22);" class="">//foo is a Int16Class</span></font></div></div><div class=""><br class=""></div><div class="">Or for creating “functions” that have properties:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; color: rgb(147, 161, 161);" class=""><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><span style="color: rgb(211, 54, 130);" class="">struct</span> add {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span> name: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">String</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">"add"</span></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> logging = <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">false</span></font></div><div style="margin: 0px; line-height: normal; color: rgb(211, 54, 130);" class=""><font face="FiraMono-Regular" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">&nbsp; &nbsp; </span>static<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span>private (set)<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span>var<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> callCount = </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">0</span></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">static</span>&nbsp;<span style="color: rgb(211, 54, 130);" class="">subscript</span>(arg1: <span style="color: rgb(39, 139, 210);" class="">Int</span>, arg2: <span style="color: rgb(39, 139, 210);" class="">Int</span>) -&gt; <span style="color: rgb(39, 139, 210);" class="">Int</span> {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">defer</span> {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">add</span>.logging == <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">true</span> {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">"callcount = </span>\<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">add</span>.callCount<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">): </span>\<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">add</span>.name<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">)(</span>\<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">(</span>arg1<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">), </span>\<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">(</span>arg2<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">)) -&gt; </span>\<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">(</span>ret<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">)"</span>)</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">add</span>.callCount += <span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">1</span></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span> ret = arg1 + arg2</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> ret</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">&nbsp; &nbsp; }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class="">}</font></div></div><div style="margin: 0px; line-height: normal;" class=""><div style="color: rgb(41, 161, 152); font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-family: FiraMono-Regular; color: rgb(211, 54, 130);" class="">let</span><span style="font-family: FiraMono-Regular; color: rgb(147, 161, 161);" class="">&nbsp;nine =&nbsp;</span><font face="FiraMono-Regular" class="">add[<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">4</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">5</span>]</font></div><div style="color: rgb(147, 161, 161); font-size: 11px; margin: 0px; line-height: normal;" class=""><font face="FiraMono-Regular" class=""><span style="color: rgb(41, 161, 152);" class="">add</span>.logging = <span style="color: rgb(211, 54, 130);" class="">true</span></font></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(211, 54, 130); font-size: 11px; font-family: FiraMono-Regular;" class="">let</span><span style="color: rgb(147, 161, 161); font-size: 11px; font-family: FiraMono-Regular;" class="">&nbsp;ten =&nbsp;</span><font face="FiraMono-Regular" class=""><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">add</span><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">[</span><span style="color: rgb(108, 113, 196); font-size: 11px;" class="">4</span><span style="color: rgb(147, 161, 161); font-size: 11px;" class="">, </span><span style="color: rgb(108, 113, 196); font-size: 11px;" class="">6</span><span style="color: rgb(41, 161, 152); font-size: 11px;" class="">]</span><span style="color: rgb(147, 161, 161); font-size: 11px;" class="">&nbsp; &nbsp; &nbsp;</span><font color="#cb4b16" class=""><span style="font-size: 11px;" class="">// prints “call count&nbsp;= 2: add(4, 6) -&gt; 10"</span></font></font></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Thoughts?</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div></div></div></div></div><div class="">
- Dave Sweeris

</div>

<br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>