[swift-evolution] [Pre-pitch] Conditional default arguments

Matthew Johnson matthew at anandabits.com
Mon Nov 27 13:12:18 CST 2017


> On Nov 27, 2017, at 12:50 PM, Douglas Gregor <dgregor at apple.com> wrote:
> 
> 
> 
>> On Nov 24, 2017, at 3:11 PM, Matthew Johnson via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> As mentioned in my prior message, I currently have a PR open to update the generics manifesto (https://github.com/apple/swift/pull/13012 <https://github.com/apple/swift/pull/13012>).  I removed one topic from that update at Doug Gregor’s request that it be discussed on the list first.  
>> 
>> 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:
>> 
>> ```swift
>> protocol Resource {
>>   associatedtype Configuration
>>   associatedtype Action
>> }
>> struct ResourceDescription<R: Resource> {
>>   func makeResource(with configuration: R.Configuration, actionHandler: @escaping (R.Action) -> Void) -> R {
>>     // create a resource using the provided configuration
>>     // connect the action handler
>>     // return the resource
>>   }
>> }
>> 
>> extension ResourceDescription where R.Configuration == Void {
>>   func makeResource(actionHandler: @escaping (R.Action) -> Void) -> R {
>>     return makeResource(with: (), actionHandler: actionHandler)
>>   }
>> }
>> 
>> extension ResourceDescription where R.Action == Never {
>>   func makeResource(with configuration: R.Configuration) -> R {
>>     return makeResource(with: configuration, actionHandler: { _ in })
>>   }
>> }
>> 
>> extension ResourceDescription where R.Configuration == Void, R.Action == Never {
>>   func makeResource() -> R {
>>     return makeResource(with: (), actionHandler: { _ in })
>>   }
>> }
>> 
>> ```
>> 
>> Adding language support for defining these more directly would eliminate a lot of boilerplate and reduce the need for overloads.
> 
> 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:
> 
>> protocol Resource {
>>   associatedtype Configuration
>>   associatedtype Action
>     func defaultConfiguration() -> Configuration
>     func defaultHandler() -> ((R.Action) -> Void)
>> }

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.  

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.

> 
>>  Doug mentioned that it may also help simplify associated type inference (https://github.com/apple/swift/pull/13012#discussion_r152124535 <https://github.com/apple/swift/pull/13012#discussion_r152124535>).
> 
> 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).

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!

> 
> 	- Doug
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171127/b9d86f8e/attachment.html>


More information about the swift-evolution mailing list