<div dir="ltr">+1</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 25, 2016 at 2:44 PM, Dave via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I’m not saying default values should be <i>required</i>, just that they be allowed if it makes sense. Plus, the way Array is defined, its generic parameter can always be inferred. In your example, `a` would be an array of IntergerLiteralType (which is currently typealiased to Int). However, it’s perfectly legal to define a struct for which the compiler can’t always infer all the generic parameters</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal"><span style="color:#bb2ca2">struct</span> Foo <T, U> {</div><div style="margin:0px;line-height:normal"> <span style="color:#bb2ca2">var</span> value: <span style="color:#703daa">T</span></div><div style="margin:0px;line-height:normal"> <span style="color:#bb2ca2">var</span> opt2ndValue: <span style="color:#703daa">U</span>?</div><div style="margin:0px;line-height:normal"> <span style="color:#bb2ca2">init</span>(<span style="color:#bb2ca2">_</span> value: <span style="color:#703daa">T</span>, opt2ndValue: <span style="color:#703daa">U</span>? = <span style="color:#bb2ca2">nil</span>) {</div><div style="margin:0px;line-height:normal"> <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">value</span> = value</div><div style="margin:0px;line-height:normal"> <span style="color:#bb2ca2">self</span>.<span style="color:#4f8187">opt2ndValue</span> = opt2ndValue</div><div style="margin:0px;line-height:normal"> }</div><div style="margin:0px;line-height:normal">}</div></div><div>In which case, the following code won’t compile:</div><div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">let</span><font face="Menlo"><span style="font-size:11px"> foo = </span></font><font face="Menlo"><font color="#4f8187"><span style="font-size:11px">Foo</span></font><span style="font-size:11px">(</span></font><span style="color:rgb(79,129,135);font-family:Menlo;font-size:11px">bar</span><span style="font-size:11px;font-family:Menlo">)</span></div></div><div>because <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">U</span> can’t be inferred. The current workaround:</div><div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">let</span><font face="Menlo"><span style="font-size:11px"> foo = </span></font><font face="Menlo"><span style="font-size:11px"><font color="#4f8187">Foo</font><Int, Whatever></span><span style="font-size:11px">(</span></font><span style="color:rgb(79,129,135);font-family:Menlo;font-size:11px">bar</span><span style="font-size:11px;font-family:Menlo">)</span></div></div><div>requires you to specify <i>all</i> the types, which means that <span style="color:rgb(79,129,135);font-family:Menlo;font-size:11px">bar</span> cannot be an inferred type, because this isn’t valid:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">let<font face="Menlo"> foo = </font><font face="Menlo"><font color="#4f8187">Foo</font></font><span style="color:#000000"><</span><span style="color:#4f8187">bar</span><span style="color:#000000">.</span>dynamicType<span style="color:#000000">, </span><span style="color:rgb(0,0,0)">Whatever</span><span style="color:#000000">>(</span><span style="color:#4f8187">bar</span><span style="color:#000000">)</span></div></div><div><br></div><div>Default values for generic parameters let you maintain type inference, <i>if</i> it would make sense in your case. If it doesn’t, then don’t provide one.</div><div><br></div><div>
- Dave Sweeris
</div>
<br><div><blockquote type="cite"><div>On Jan 25, 2016, at 01:00, Howard Lovatt <<a href="mailto:howard.lovatt@gmail.com" target="_blank">howard.lovatt@gmail.com</a>> wrote:</div><br><div><div dir="auto"><div>+1 for labels, I think generics are special arguments to `inits` and therefore labels make sense to me.</div><div><br></div><div>-1 for default values, the value is inferred if not specifically specified. What would:</div><div><br></div><div> let a = [1]</div><div><br></div><div>be? An `Int` array or an array of whatever the default generic type for an array is?<br><br>Sent from my iPad</div><div><div class="h5"><div><br>On 24 Jan 2016, at 7:38 AM, David Sweeris via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div>The title seems fairly self-explanatory. Does anyone else think this could be useful?<div><div style="margin:0px;line-height:normal"><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">struct</span><span style="font-family:Menlo;font-size:11px"> True : </span><font color="#703daa" face="Menlo"><span style="font-size:11px">BooleanType</span></font><font face="Menlo"><span style="font-size:11px"> {…} </span></font><font color="#008400" face="Menlo"><span style="font-size:11px">// Just part of the example… n</span></font><span style="font-size:11px;color:rgb(0,132,0);font-family:Menlo">ot in the proposal (although I do like it)</span></div><div style="margin:0px;line-height:normal"><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px">struct</span><span style="font-family:Menlo;font-size:11px"> False : </span><font color="#703daa" face="Menlo"><span style="font-size:11px">BooleanType</span></font><font face="Menlo"><span style="font-size:11px"> {…} </span></font><font color="#008400" face="Menlo"><span style="font-size:11px">// Same</span></font></div></div><div style="margin:0px;line-height:normal"><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px">// This is where the actual idea starts</span></div><div><div style="margin:0px;line-height:normal"><div style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="color:#bb2ca2">struct</span> BigInt <BaseType: T = <span style="color:rgb(112,61,170)">Int</span>, CanEqualZero: U = <span style="color:rgb(79,129,135)">Yes </span><span style="color:rgb(187,44,162)">where</span> T: IntegerArithmeticType, U: BooleanType> {…}</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">The first parameter label could be skipped (or not), depending on whatever the rules for functions parameter labels ends up being (either way, they should be the same IMHO). Then variables could be declared like this:</div><div style="margin:0px;line-height:normal"><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span style="color:#bb2ca2">let</span> foo = <span style="color:#4f8187">BigInt</span>()<span style="white-space:pre-wrap">        </span> <span style="color:rgb(0,132,0)">// BigInt<Int, No>()</span></div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><font face="Menlo" style="font-size:11px"><span style="color:rgb(187,44,162)">let</span><font> bar = </font><span style="color:rgb(79,129,135)">BigInt</span><font><Int32>() </font><span style="color:rgb(0,132,0)">// For when your data will be processed on a 32-bit platform or something</span></font></div></div></div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">let</span><font face="Menlo"><span style="font-size:11px"> divisor = </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(79,129,135)">BigInt</span><font face="Menlo"><span style="font-size:11px"><CanEqualZero: </span></font><span style="color:rgb(79,129,135);font-family:Menlo;font-size:11px">False</span><font face="Menlo"><span style="font-size:11px">>()</span></font></div><div style="font-family:Menlo;font-size:11px"><br></div></div></div><div>(The obvious follow-up suggestion is to then allow a generic type’s definition to change based on the results of logical operations performed purely on the types that are passed in, but I think that’s getting into “macro system” territory, and should probably be its own thing.)</div><div><br></div><div>Anyway, thoughts?</div></div></div><br><div>
- Dave Sweeris
</div>
<br></div></blockquote></div></div><span class=""><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></span></div></div></blockquote></div><br></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>