> On Nov 27, 2017, at 3:27 PM, Tony Allevato <tony.allev...@gmail.com> wrote:
> 
> On Mon, Nov 27, 2017 at 12:49 PM Matthew Johnson <matt...@anandabits.com 
> <mailto:matt...@anandabits.com>> wrote:
>> On Nov 27, 2017, at 1:24 PM, Tony Allevato <tony.allev...@gmail.com 
>> <mailto:tony.allev...@gmail.com>> wrote:
>> 
>> 
>> 
>> On Mon, Nov 27, 2017 at 11:12 AM Matthew Johnson via swift-evolution 
>> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>>> On Nov 27, 2017, at 12:50 PM, Douglas Gregor <dgre...@apple.com 
>>> <mailto:dgre...@apple.com>> wrote:
>>> 
>>> 
>>> 
>>>> On Nov 24, 2017, at 3:11 PM, Matthew Johnson via swift-evolution 
>>>> <swift-evolution@swift.org <mailto:swift-evolution@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.  
>> 
>> Out of curiosity, what part of my proposed syntax misses the mark for your 
>> use case?
> 
> I think it’s important that we be clear about when a default is and is not 
> available.  Your syntax makes the availability of a default non-local.
> 
> That's certainly true. It can be mitigated somewhat by placing the 
> definitions as close to the declaration as possible, but it's still up to the 
> user to do the right thing here:
> 
> ```
> struct ResourceDescription<R: Resource> {
>   func defaultConfiguration() -> Void { return () }
>   func defaultActionHandler() -> (Never) -> Void { return _ in }
>   func makeResource(...) {
>     ...
>   }
> }
> ```
> 
> It's worth noting that this problem does exist somewhat in the language 
> today, if you replace "makes the availability of a default non-local" with 
> "makes the value of a default non-local”.

Non-locality of the value doesn’t bother me.  The availability of the default 
is part of the API contract, the specific value need not be.

> 
>  
> 
>> 
>> The parts that I think are somewhat unfortunate are losing a small bit of 
>> reference locality and the introduction of a new operator to distinguish 
>> between "default argument not present if no match found" vs. “com pile time 
>> error", definitely. However, one unfortunate trend I've noticed when new 
>> features are proposed is that there's a tendency to end up with "let's 
>> invent @yet_another_attribute" and that just doesn't seem scalable or clean 
>> if we can strive to better integrate it into the syntax of the language.
> 
> Xiaodi acknowledged that his suggestion is a bit clunky but I’m not sure we 
> can do better while preserving clarity and making the defaults part of the 
> function declaration.  If we added this feature it would be relatively 
> advanced and usually used by library developers.  While I would be pleased 
> with more elegant syntax I’m not sure there is a better solution.  I prefer 
> semantic clarity to syntactic elegance.
> 
> Why not both? Hopefully we can get some more input from folks who might have 
> other ideas about how to bring the two ideas together.

Of course I agree!  It would be wonderful if someone figured out how to 
accomplish that.

> 
> For me, grafting on a second completely different default value syntax for 
> functions is hard to swallow, especially if folks feel that it's clunky right 
> out of the gate. (Especially if we're talking about parsing entire 
> expressions—and closure expressions—out of an attribute body. How deep does 
> that need to go?)

I can sympathize with that point of view.  The syntactic problem is that we 
don’t have an obvious way to attach constraints to default arguments inline, 
especially if different defaults were available for the same parameter under 
different constraints.  We could place an attribute on the default itself like 
this:

`configuration: Configuration = @where((), Configuration == Void), 
@where(R.defaultConfiguration, R: DefaultConfigurable)`

However, this puts a lot of noise inline.  I think Xiaodi’s suggestion of 
attaching an attribute to the function itself would lead to more readable 
declarations.

> 
> My thoughts on this particular problem are either that it's a relatively 
> advanced problem that comes up rarely enough that using extensions isn't a 
> significant problem and adding a special annotation isn't worth the increase 
> in compiler maintenance/complexity, or it's more common than we think and 
> thus warrants a syntax better integrated with the rest of the language. It's 
> probably hard to say for sure where this problem falls among those two, but 
> my guess it's that it's closer to the former than the latter. If that's the 
> case, I'd rather stick with what we have today rather than create an ad hoc 
> annotation for a rare use case.

That’s a reasonable point of view.  I agree that it’s hard to say which case 
this falls under in general.  

It’s worth noting that any parameter of an unconstrained generic type which 
will often be Void in practice is a parameter for which this feature leads to 
cleaner call-site code.  It is often reasonable to provide nil as a default for 
Optional parameters.  I don’t know of a way to identify whether an 
unconstrained T is an Optional but if that was possible this would be another 
relatively common use case for the feature. 

>  
> 
>> 
>> I'm particularly interested in this because I hit a use case that's similar 
>> to yours in my own code base. I wanted a generic type that could be 
>> instantiated with a disjoint type set—either a RawRepresentable whose 
>> RawValue is Int, or an Int itself. This could have been solved by having Int 
>> retroactively conform to RawRepresentable, but doing that to a fundamental 
>> built-in type Feels Dirty<emoji_u2122.png>, so I made it work by not 
>> constraining the generic type at all and moving the public initializers to 
>> constrained extensions so that the only way you could *instantiate* the type 
>> is if you have a correct type argument, and those initializers pass through 
>> to an internal one, sending along a closure that does the correct 
>> transformation: 
>> https://github.com/allevato/icu-swift/blob/master/Sources/ICU/RuleBasedBreakCursor.swift#L208
>>  
>> <https://github.com/allevato/icu-swift/blob/master/Sources/ICU/RuleBasedBreakCursor.swift#L208>
>> 
>> With your proposed addition, I could hoist those closures into default 
>> arguments based on the constraints instead of adding extensions.
> 
> IIUC you would still need the extensions because you only want to expose an 
> initializer for two specific type arguments.  Conditional default arguments 
> would only work if you didn’t mind exposing an initializer for other type 
> arguments (while requiring an explicit ruleStatusFactory for other type 
> arguments).
> 
> True, and I wouldn't mind that—the Int and RawRepresentable versions would 
> essentially be conveniences in that case.

You can have that today if you expose the initializer you're forwarding to.

>  
> 
>> 
>> That being said, I don't find the extension-based approach *that* 
>> restrictive, and it kind of makes sense from the point of view of "this 
>> overload only exists for this type under a particular constraint". I wonder 
>> how often this comes up that combinatorial explosion is truly harmful.
> 
> The extension based approach is workable, it’s just verbose and clunky and I 
> suspect it results in useful default arguments being omitted in some cases.  
> I’m sure the combinatorial problem is relatively rare but it leads to an 
> overload set that is difficult to maintain properly when it happens.  That 
> said, if the community decides this is a niche problem that isn’t worth 
> language support to solve I would understand that decision.
> 
> To be honest, this thread has received more positive feedback than I expected 
> (I expected the generalized supertype thread to get more traffic).  That 
> indicates to me that the feature might not be quite as niche as I suspected.  
> That is one of the things I hoped to learn from the thread.
> 
>> 
>>  
>> 
>> 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
>>> 
>>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution@swift.org <mailto:swift-evolution@swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution 
>> <https://lists.swift.org/mailman/listinfo/swift-evolution>

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to