<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 27, 2017, at 12:50 PM, Douglas Gregor <<a href="mailto:dgregor@apple.com" class="">dgregor@apple.com</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; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Nov 24, 2017, at 3:11 PM, Matthew Johnson 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="">As mentioned in my prior message, I currently have a PR open to update the generics manifesto (<a href="https://github.com/apple/swift/pull/13012" class="">https://github.com/apple/swift/pull/13012</a>). I removed one topic from that update at Doug Gregor’s request that it be discussed on the list first. </div><div class=""><br class=""></div><div class="">The idea is to add the ability to make default arguments conditional (i.e. depend on generic constraints). It is currently possible to emulate conditional default arguments using an overload set. This is verbose, especially when several arguments are involved. Here is an example use case using the overload method to emulate this feature:</div><div class=""><br class=""></div><div class="">```swift</div><div class="">protocol Resource {</div><div class=""> associatedtype Configuration</div><div class=""> associatedtype Action</div><div class="">}</div><div class="">struct ResourceDescription<R: Resource> {</div><div class=""> func makeResource(with configuration: R.Configuration, actionHandler: @escaping (R.Action) -> Void) -> R {</div><div class=""> // create a resource using the provided configuration</div><div class=""> // connect the action handler</div><div class=""> // return the resource</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">extension ResourceDescription where R.Configuration == Void {</div><div class=""> func makeResource(actionHandler: @escaping (R.Action) -> Void) -> R {</div><div class=""> return makeResource(with: (), actionHandler: actionHandler)</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">extension ResourceDescription where R.Action == Never {</div><div class=""> func makeResource(with configuration: R.Configuration) -> R {</div><div class=""> return makeResource(with: configuration, actionHandler: { _ in })</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">extension ResourceDescription where R.Configuration == Void, R.Action == Never {</div><div class=""> func makeResource() -> R {</div><div class=""> return makeResource(with: (), actionHandler: { _ in })</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">```</div><div class=""><br class=""></div><div class="">Adding language support for defining these more directly would eliminate a lot of boilerplate and reduce the need for overloads. </div></div></div></blockquote><div class=""><br class=""></div><div class="">If one could refer to `self` in a default argument (which is not a big problem), you could turn the default into a requirement itself… although it doesn’t *quite* work with your example as written because it would always need to be implemented somehow:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class="">protocol Resource {</div><div class=""> associatedtype Configuration</div><div class=""> associatedtype Action</div></div></blockquote> func defaultConfiguration() -> Configuration</div><div class=""> func defaultHandler() -> ((R.Action) -> Void)<br class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class="">}</div></div></blockquote></div></div></div></div></div></blockquote><div><br class=""></div><div>This won’t work on its own for this use case because there is only a valid default in relatively narrow (but common) cases. For most values of Configuration and Action an argument must be provided by the caller. Xiaodi’s proposed syntax is the best fit (so far) for the use case I had in mind. </div><div><br class=""></div><div>That said, the ability to refer to self in default arguments is complementary as it would expand the cases where conditional default arguments could be provided. For example, in the example above it would allow a resource to provide a nontrivial default configuration.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""> Doug mentioned that it may also help simplify associated type inference (<a href="https://github.com/apple/swift/pull/13012#discussion_r152124535" class="">https://github.com/apple/swift/pull/13012#discussion_r152124535</a>).</div></div></div></blockquote><br class=""></div><div class="">Oh, I thought this was something related to choosing a defaults for associated types, which might have helped with my current associated-type-inference quandary. The topic you actually wanted to discuss is disjoint (sorry).</div></div></div></div></blockquote><div><br class=""></div><div>I was wondering how it was related and wondered if it was somehow due to reduction in the size of the overload set. If you have any ideas on changes that might help guide the inference algorithm somehow please start a new thread. Even if you’re only able to describe the challenges it might be worth a thread if it’s possible others might have useful ideas. Improving the reliability and predictability of inference is a very important topic!</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div class=""><br class=""></div><br class=""></div></div></div></blockquote></div><br class=""></body></html>