<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Mon, Nov 27, 2017 at 10:32 PM Slava Pestov &lt;<a href="mailto:spestov@apple.com">spestov@apple.com</a>&gt; 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;line-break:after-white-space">Hi Tony,<div><br></div><div>So if my understanding is correct, the basic proposal is the following:</div><div><br></div><div>func id&lt;T&gt;(t: T ?= T.defaultValue) { return t }</div><div><br></div><div>extension Int { static var defaultValue = 0 }</div><div><br></div><div>extension String { static var defaultValue = “” }</div><div><br></div><div>id() as Int // returns 0</div><div><div>id() as String // returns “”</div><div>id() as SomeRandomType // fails to type check — no default argument</div><div><br></div><div>I don’t understand what would happen if the caller is itself generic though, for example:</div><div><br></div><div>callsID&lt;T&gt;(_ t: T) {</div><div>  _ = id() as T</div><div>}</div><div><br></div><div>It appears that body of callsID() itself cannot type check without knowledge of the concrete T that will be used with this function.</div></div></div></blockquote><div><br></div><div>Thanks for bringing up this example, Slava.</div><div><br></div><div>Unless I&#39;m misunderstanding, the issue you&#39;re describing is inherent to the *problem* described in the original post, not to any specific hypothetical syntax for adding the default arguments, correct? In other words, if this was written using extensions as can be done today:</div><div><br></div><div>```</div><div>struct Foo&lt;T&gt; {</div><div>  func id(t: T) -&gt; T { return t }</div><div>}</div><div><br></div><div>extension Foo where T == Void {</div><div>  func id() -&gt; T { return () }</div><div>}</div><div><br></div><div>extension Foo where T == Int {</div><div>  func id() -&gt; T { return 0 }</div><div>}</div><div><br></div><div>callsID&lt;T&gt;(_ t: T) {</div><div>  _ = Foo().id() as T    // mark</div><div>}</div><div>```</div><div><br></div><div>The compiler would still reject the marked line because there&#39;s no guarantee that T is one of the types that has the necessary overload.</div><div><br></div><div>But now that you&#39;ve mentioned it, it does have me thinking that this problem might be better left to extensions. In one sense, default arguments are a kind of &quot;overload synthesis&quot;, but on the other hand, there&#39;s an expectation that the default value expression is of a single type (or set of related types) known at compile time. Even if it&#39;s generic, it still must be expressed in terms of whatever constraints are present on that generic type—you can&#39;t use a disjunction of types, but instead have to have a common protocol that would provide some operation.</div><div> </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;line-break:after-white-space"><div><div><br></div><div>Slava</div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><br><blockquote type="cite"><div>On Nov 27, 2017, at 4:10 PM, Tony Allevato via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-1835373692816656522Apple-interchange-newline"><div><div style="font-family:Helvetica;font-size:12px;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">I totally agree that that&#39;s a good rule in general—I&#39;m not 100% comfortable making an exception to it for this, but I wanted to start a discussion about a different approach than had been considered so far.</div><div style="font-family:Helvetica;font-size:12px;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"><br></div><div style="font-family:Helvetica;font-size:12px;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">The idea of forcing the user to acknowledge the explicitness of SFINAE with a strawman syntax `=?` instead of `=` was a thought experiment to bridge the wild-west-C++ world of templates and Swift&#39;s stricter generics, but I can definitely understand if even that kind of approach is something that the core team (who are far more familiar with the C++ side of that coin than I am) doesn&#39;t wish to support. As was pointed out, it&#39;s not something Swift supports anywhere else today.</div><div style="font-family:Helvetica;font-size:12px;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"><br></div><div style="font-family:Helvetica;font-size:12px;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">If we look at it from that point of view, where such a semantic treatment of generics would not be supported, I think it becomes a lot harder to rationalize treating this as &quot;default arguments&quot;. What you really do have (and what writing it as constrained extensions makes clear) is additional overloads, because they only apply to certain subsets of types. If that&#39;s the case, maybe it&#39;s the wrong approach to try to turn overloads into &quot;partial default values&quot;.</div></div></blockquote></div><br></div></div></blockquote></div></div>