-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
[SR-142] mutating function in protocol extension erroneously requires var
declaration of class variables
#42764
Comments
The issue is also there in Apple Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f829) |
|
For what it's worth, there is a (verbose) workaround, which is to redeclare the mutating method in the adopting class:
|
Looked into this briefly, and it's a lot harder than just changing the error checking, since m() has an implicit `inout P` first argument. Probably the solution is an automatic version of Gwendal's workaround: declaring a hidden `var p: P = c` and calling the method on p. |
Thanks for the investigation, Greg! |
Automatically declaring a hidden var like that would cause surprising behavior when the
With the automatic hidden variable, the |
Thanks Kevin. I overlooked self assignment in default implementation, which requires the implementation described by Greg. So it isn't a bug, but actually a feature, and anyone bothered by this behavior should discuss it on the swift-evolution mailing list, isn't it? |
Comment by Josh Avant (JIRA) For posterity, here's a link to additional conversation about this on swift-evolution: |
As Kevin explained this is not a bug, because the method implementation may assign to self. |
Environment
Version 7.1.1 (7B1005)
Additional Detail from JIRA
md5: c88df0cab57ceac17b5038fd1dff4b54
is duplicated by:
var
relates to:
Issue Description:
Hello,
The following code won't compile, when it should:
This error should not exist: the type of variable
c
isC
. Mutating methods can not be added toC
, because of the'mutating' isn't valid on methods in classes or class-bound protocols
error. Hence themutating
qualifier ofP.m
should not apply toc
, andc
should be able to be declared as alet
variable.The text was updated successfully, but these errors were encountered: