-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default arguments cannot be used if they would define another generic parameter's type. #65035
Comments
I think this is intended behavior. Consider your last and most basic example:
Given the default argument will not participate in the inference of cc @xedin |
For the last case, the default expression makes the generic type on the second parameter entirely redundant. That's not true for the original example:
The first parameter specifies the type |
No, if it compiled, it would replace these two overloads: func ƒ<T>(_: T, _: T) { }
func ƒ(_: Void) { } à la func ƒ<T, U>(_: T = (), _: U = ()) { } |
This is intentional, these cases are outlined in the proposal, also note that |
Given the partial implementation in the compiler for the below code, I don't believe in the "intentionality", but if it exists, the bug is that it needs to be better advertised so that nobody else wastes your time about it in the future. I don't see it.
This is partially broken, in that it can compile in this form, but cannot be used: extension Protocol {
func ƒ<Property>(
property: (Self) -> Property = \Self.property // Will not compile without explicit `Self`
) {
_ = property(self)
}
}
struct Struct: Protocol {
var property: Never { fatalError() }
}
Struct().ƒ() // Generic parameter 'Property' could not be inferred |
I'm not sure what you mean, can you elaborate on how in your opinion this advertisement should work?
|
Regardless of how confusing these rules may appear — I will probably fail to name all the nooks and crannies myself — I sympathize with Jessy in that we ought to have the book set the rules and clarify the behavior, which AFAIU is supposed to be the primary source of truth regarding language semantics. From what I gather the expected behavior is:
There should be better ways to phrase some of these, of course. |
Given
This should be expressible as:
…but that error occurs instead.
Workaround 1—Explicit Overloads:
(Somewhat inequivalent) Workaround 2—Existential:
Note: it's not necessary for a closure to be involved.
The text was updated successfully, but these errors were encountered: