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-6636] Constant and Variable shadowing produces confusing Program Bugs #49185

Open
swift-ci opened this issue Dec 18, 2017 · 0 comments
Open

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-6636
Radar None
Original Reporter aaroncrespo (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s
Labels Improvement
Assignee None
Priority Medium

md5: 466deddb9da15fc888b2f9f4d07edafb

relates to:

  • SR-8524 Shadowing in protocols leads to difficult to find bugs

Issue Description:

Another Team had some trouble tracking down a bug that was finally ruled to be shadowing between a var in a protocol and a let in the implementation. This is made worse than binding shadowing because the protocol might be defined "father" away than the implementation.

protocol CuteAnimal {
  var name: String { get }
}

extension CuteAnimal {
  var name: String { return "Porg" }
}

struct CrystalFoxThing: CuteAnimal {
  let name: String?
  init(name: String) {
    self.name = name
  }
}


let f = CrystalFoxThing(name: "Fox")

func useAnimal(animal: CuteAnimal) {
  print(animal.name)
}

useAnimal(animal: f)

I was also surprised to find that:

protocol CutePetAnimal: CuteAnimal {   var name: String { set } }

Was not allowed but

protocol CutePetAnimal: CuteAnimal {   var name: String { get set } }

Is allowed, which to me adds yet another level of confusing shadowing (I would have expected the
reverse to be true, IE you are extending name with a getter)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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

1 participant