<div style="white-space:pre-wrap">Sorry, can you explain what you mean when you say you must have a setter? Why would you mutate the view and not the array itself (`foo[42] = .double(42)` as opposed to `foo.double[42] = 42`)?<br></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Nov 18, 2016 at 03:25 Adrian Zubarev via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="m_-6060179511150569908bloop_markdown gmail_msg"><p class="gmail_msg">Thank you guys for all your suggestions so far.</p>
<p class="gmail_msg">I understand the idea behind the generic subscript here, they are neat and highly needed, but even this approach won’t solve my issue of clarity here.</p>
<p class="gmail_msg">The Array I extend here has an <code class="gmail_msg">Element</code> of type <code class="gmail_msg">Value</code> which is an enum that wraps other types around (part of BSON).</p>
<p class="gmail_msg">I’d have to insert a huge pattern matching switch into that generic subscript and unwrap every possible type. Don’t get me wrong, this would work, because the result type is an optional, where I just can return nil if nothing matches.</p>
<p class="gmail_msg">But again I lose the clarity from the readers prospective, because I don’t know by reading code like <code class="gmail_msg">array[at: 123] = someValue</code> what kind of subscript I’m using here. </p>
<p class="gmail_msg">As already suggested, the view workaround would result in the exact the same syntax I look for, but it has it own downsides as I already mentioned (+ every time you’d need to instantiate a new view).</p>
<p class="gmail_msg"></p></div><div class="m_-6060179511150569908bloop_original_html gmail_msg"></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="m_-6060179511150569908bloop_original_html gmail_msg"><div id="m_-6060179511150569908bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto" class="gmail_msg"><br class="gmail_msg"></div> <br class="gmail_msg"> <div id="m_-6060179511150569908bloop_sign_1479460311132606976" class="m_-6060179511150569908bloop_sign gmail_msg"><div style="font-family:helvetica,arial;font-size:13px" class="gmail_msg">-- <br class="gmail_msg">Adrian Zubarev<br class="gmail_msg">Sent with Airmail</div></div> <br class="gmail_msg"></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="m_-6060179511150569908bloop_original_html gmail_msg"><p class="m_-6060179511150569908airmail_on gmail_msg">Am 18. November 2016 um 09:55:00, Haravikk (<a href="mailto:swift-evolution@haravikk.me" class="gmail_msg" target="_blank">swift-evolution@haravikk.me</a>) schrieb:</p> <blockquote type="cite" class="m_-6060179511150569908clean_bq gmail_msg"><span class="gmail_msg"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"></div><div class="gmail_msg">
<div class="gmail_msg">Could this be addressed by allowing generic
constraints on subscripts?</div>
<div class="gmail_msg">For example, with methods we can currently do:</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px" class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg">
<div style="margin:0px;line-height:normal" class="gmail_msg">
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">struct</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">Foo
{</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">var</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">values:[</span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">Any</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">] =
[]</span></div>
<div style="margin:0px;line-height:normal;min-height:13px" class="gmail_msg"><br class="gmail_msg"></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">func</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">get<T>(at:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)
-></span> <span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">T</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">?
{</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">return</span> <span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">values</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">indices</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81" class="gmail_msg">contains</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(at)
?</span> <span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">values</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">[at]</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">as</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">?</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">T</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">:</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">nil</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div>
<div style="margin:0px;line-height:normal;min-height:13px" class="gmail_msg"><br class="gmail_msg"></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">func</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">get<T>(at:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, as
theType:</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">T</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.Type)
-></span> <span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">T</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">?
{</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">return</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">values.indices.contains(at) ? values[at]</span> <span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">as</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">?</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">T</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">:</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">nil</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div>
<div style="margin:0px;line-height:normal;min-height:13px" class="gmail_msg"><br class="gmail_msg"></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">mutating</span> <span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">func</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">set<T>(at:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,
to:</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">T</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)
{</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">if</span> <span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">values</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">indices</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#3d1d81" class="gmail_msg">contains</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(at)
{</span> <span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">values</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">[at] = to
}</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div>
<div style="margin:0px;line-height:normal;min-height:13px" class="gmail_msg"><br class="gmail_msg"></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">foo =</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(values:
[</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">1.5</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">2.5</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">3.5</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">1</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">2</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">3</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">])</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">a =
foo.get(at:</span> <span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">0</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, as:
Double.</span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">self</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">b:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Double</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">=</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="gmail_msg">get</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(at:</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">1</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)!</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">c:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? =</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="gmail_msg">get</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(at:</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">2</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">d =
foo.get(at:</span> <span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">3</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, as:
Double.</span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">self</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">e:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">=</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="gmail_msg">get</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(at:</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">4</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)!</span></div>
<div style="margin:0px;line-height:normal" class="gmail_msg">
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">f =
foo.get(at:</span> <span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">5</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, as:
Int.</span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">self</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">)</span></div>
</div>
</div>
</blockquote>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="gmail_msg"></div>
i.e- the type is inferred from the call-site either with an
explicit variable type, or by passing in the expected type as the
second argument, which I think is a pretty neat way to do it.</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg">If we could do the same with subscripts we could do
something like:</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px" class="gmail_msg">
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">struct</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">Foo
{</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">var</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">values:[</span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">Any</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">] =
[]</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="gmail_msg"><br class="gmail_msg"></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">subscript</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><T>(</span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">_</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">at:Int)
-> T? {</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">get {</span> <span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">return</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">values.indices.contains(at) ? values[at]</span> <span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">as</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? T :</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">nil</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">set {</span> <span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">if</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">values.indices.contains(at) { values[at] = newValue }
}</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="gmail_msg"><br class="gmail_msg"></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">subscript</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><T>(</span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">_</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">at:Int, as
theType:T.Type) -> T? {</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">return</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">values.indices.contains(at) ? values[at]</span> <span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">as</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? T :</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">nil</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">}</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="gmail_msg"><br class="gmail_msg"></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">foo =</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">(values:
[</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">1.5</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">2.5</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">3.5</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">1</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">2</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">,</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">3</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">])</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">a =</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">[</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">0</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, as:</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Double</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">self</span>]</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">b:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Double</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">=</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">[</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">1</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">]!</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">c:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">? =</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">[</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">2</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">]</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">d =</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">[</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">3</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, as:</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Double</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">self</span>]</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">e:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">=</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">[</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">4</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">]!</span></div>
</div>
<div class="gmail_msg">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="gmail_msg">let</span> <span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">f =</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">foo</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">[</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">5</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">, as:</span>
<span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="gmail_msg">self</span>]</span></div>
</div>
</blockquote>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg">Are generic constraints on subscripts part of the
generics manifesto?</div>
<div class="gmail_msg"><br class="gmail_msg"></div>
<div class="gmail_msg">
<div class="gmail_msg">
<blockquote type="cite" class="gmail_msg">
<div class="gmail_msg">On 17 Nov 2016, at 20:14, Adrian Zubarev via
swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:</div>
<br class="m_-6060179511150569908Apple-interchange-newline gmail_msg">
<div class="gmail_msg">
<div class="m_-6060179511150569908bloop_markdown gmail_msg" style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<p style="margin:15px 0px" class="gmail_msg">
Dear Swift community,</p>
<p style="margin:15px 0px" class="gmail_msg">while building a framework
for BSON I had the following idea.</p>
<p style="margin:15px 0px" class="gmail_msg">Here is a snippet of some
code I do have in my module:</p>
<pre style="margin:15px 0px;font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(204,204,204);overflow:auto;padding:4px 8px;word-break:normal;word-wrap:normal" class="gmail_msg"><code class="m_-6060179511150569908swift gmail_msg" style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:0px;margin:0px;padding:0px;word-break:normal;word-wrap:normal">extension Array where Element == Document.Value {
public func double(at index: Int) -> Double? {
guard self.startIndex <= index && index < self.endIndex else { return nil }
if case .double(let double) = self[index] {
return double
}
return nil
}
…
}
</code></pre>
<p style="margin:15px 0px" class="gmail_msg">This function is used to
query the array and check if the element at the given index is of a
specific type. Now I would like also to implement a semi-schema
setter.</p>
<p style="margin:15px 0px" class="gmail_msg">The problem that I see, is
the ugliness of the<span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span><code style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal" class="gmail_msg">subscript</code><span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span>I’d create.</p>
<p style="margin:15px 0px" class="gmail_msg">Currently the code would read
nicely<span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span><code style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal" class="gmail_msg">let d = array.double(at: 42)</code>, but after change to
a<span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span><code style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal" class="gmail_msg">subscript</code><span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span>the API would look
odd<span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span><code style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal" class="gmail_msg">array[doubleAt: 42] = 5.0</code>.</p>
<p style="margin:15px 0px" class="gmail_msg">Don’t get me wrong here, I
also have methods with larger names like<span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span><code style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal" class="gmail_msg">public func scopedJavaScript(at index: Int) ->
…</code>. You can easily imagine that such subscripts would look
ugly<span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span><code style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal" class="gmail_msg">array[scopedJavaScriptAt: 123] = …</code>.</p>
<p style="margin:15px 0px" class="gmail_msg">I propose to align the design
of subscript with functions where one could optionally give
subscript a name.</p>
<pre style="margin:15px 0px;font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(204,204,204);overflow:auto;padding:4px 8px;word-break:normal;word-wrap:normal" class="gmail_msg"><code class="m_-6060179511150569908swift gmail_msg" style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:0px;margin:0px;padding:0px;word-break:normal;word-wrap:normal">func name(label parameter: Type) -> ReturnType
subscript optionalName(label parameter: Type) -> ReturnType
</code></pre>
<p style="margin:15px 0px" class="gmail_msg">This change would make my API
nice and clean.<span class="m_-6060179511150569908Apple-converted-space gmail_msg"> </span><code style="font-family:Menlo,Consolas,'Liberation Mono',Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal" class="gmail_msg">array.scopedJavaScript[at: 213] = …</code></p>
<p style="margin:15px 0px" class="gmail_msg">This also might be the
opportunity to rethink the labeling rule on subscripts, but this
shall not be the main focus of this pitch.</p>
<div style="margin:15px 0px" class="gmail_msg"><br class="m_-6060179511150569908webkit-block-placeholder gmail_msg"></div>
</div>
<div class="m_-6060179511150569908bloop_original_html gmail_msg" style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<div id="m_-6060179511150569908bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px;margin:0px" class="gmail_msg"><br class="gmail_msg"></div>
<br class="gmail_msg">
<div id="m_-6060179511150569908bloop_sign_1479412498260118016" class="m_-6060179511150569908bloop_sign gmail_msg">
<div style="font-family:helvetica,arial;font-size:13px" class="gmail_msg">-- <br class="gmail_msg">
Adrian Zubarev<br class="gmail_msg">
Sent with Airmail</div>
</div>
</div>
<div class="m_-6060179511150569908bloop_markdown gmail_msg" style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<div style="margin:15px 0px" class="gmail_msg"><br class="m_-6060179511150569908webkit-block-placeholder gmail_msg"></div>
</div>
<span style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254);float:none;display:inline!important" class="gmail_msg">_______________________________________________</span><br style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)" class="gmail_msg">
<span style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254);float:none;display:inline!important" class="gmail_msg">swift-evolution mailing list</span><br style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)" class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" style="color:rgb(65,131,196);background-color:rgb(254,254,254);text-decoration:none;font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)" class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="color:rgb(65,131,196);background-color:rgb(254,254,254);text-decoration:none;font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)" class="gmail_msg"></div>
</blockquote>
</div>
<br class="gmail_msg"></div>
</div></div></span></blockquote></div></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>