Skip to content
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-4541] Class-Only protocol inheriting from normal protocol require to be var to allow assign to their properties. #47118

Closed
swift-ci opened this issue Apr 8, 2017 · 1 comment

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Apr 8, 2017

Previous ID SR-4541
Radar None
Original Reporter erne (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

OS X 10.11.6 (15G1217)
Xcode Version 8.2.1 (8C1002)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 160ee33eef73361ca49d17b79be58103

is duplicated by:

  • SR-11298 Writable property declaration in a conditional-conforming protocol extension has incorrect mutability

relates to:

  • SR-142 mutating function in protocol extension erroneously requires var declaration of class variables

Issue Description:

In this scenario:

protocol CoolProtocol {
    var coolness: Int { get set }
}

protocol CoolClassProtocol: class, CoolProtocol {
   // if you re-declare coolness here all works as expected
   // var coolness: Int { get set }
}

class Cool {
    var coolness = 1
}

func incrementCool(_ cool: CoolClassProtocol) {
   // here you get compiler error saying cool should be var.
   // if you do it as in: 
   // var cool = cool
   // you'll get a warning telling you that cool is never mutated and should be made a let constant.
    cool.coolness += 1
}
@slavapestov
Copy link
Member

@slavapestov slavapestov commented Apr 23, 2017

This is working as intended.

Imagine if the CoolProtocol had a default implementation of coolness in a protocol extension:

extension CoolProtocol {
  var coolness: Int { get { return 0 } set { self = ... } }
}

Here it's assigning to self (the conforming type) so it really does have to be a var, even if its a class.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants