<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 have wanted something similar to this for a while, and haven’t suggested it for a while for the same reason… I can’t really think of a good syntax.<div class=""><br class=""></div><div class="">My best idea so far is to put it after the function declaration but before the open brace:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func myCrazyFunction<T>(myParam: T)->Int</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@default myParam = 0 where T == Int.Self</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@default myParam = “” where T == String.Self</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>//Function body goes here</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class="">When multiple options are available, it would match the most specific one that qualifies. The idea I had would also allow it to have expressions on the rhs, and would potentially allow other input types outside of T (as long as the rhs returns T):</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func evenCrazier(myParam: String)->Int</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@default myParam = “\($0)” where myParam : CustomStringConvertible</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>//Function body goes here</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class="">There are some obvious issues with the syntax here (e.g. I am using $0), but I think the general idea is a really useful one. That is, I am able to provide a conversion formula that lets someone pass anything which is customStringConvertible to this String parameter and have it pass the appropriate String to my actual function. I would use this constantly, and it would prevent the combinatorial explosion of overloads I have now…</div><div class=""><br class=""></div><div class="">This also gets rid of the need for the often-requested-but-never-going-to-happen union types:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>enum MyEnum {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>case int (Int)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>case str (String)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func noNeedForUnion(_ intOrStr: MyEnum)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@default intOrStr = .int($0) where intOrStr == Int.Self</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@default intOrStr = .str($0) where intOrStr == String.Self</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>//Function body here</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>noNeedForUnion(3) //This passes .int(3)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>noNeedForUnion(“Hey”) //this passes .str(“Hey”)</div><div class=""><br class=""></div><div class="">I could see this making a bunch of frameworks nicer to use… (or at least nicer to write)</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div class=""><br class=""><div><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. 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 class=""><br class=""></div><div class="">The reason that I call this a pre-pitch and one reason Doug requested it be discussed on list is that I haven’t thought of a good way to express this syntactically. I am interested in hearing general feedback on the idea. I am also looking for syntax suggestions.</div><div class=""><br class=""></div><div class="">Matthew</div></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=""></div></body></html>