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-5475] Can't set property declared optional in protocol #48047

Open
mattneub opened this issue Jul 17, 2017 · 3 comments
Open

[SR-5475] Can't set property declared optional in protocol #48047

mattneub opened this issue Jul 17, 2017 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@mattneub
Copy link

Previous ID SR-5475
Radar rdar://problem/17498958
Original Reporter @mattneub
Type Bug
Environment

Xcode Version 9.0 beta 3 (9M174d)

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

md5: 89a4dcbcf56d5035b26555315b427f3c

is duplicated by:

  • SR-6134 Incorrect error message when accessing @optional properties from a class/subtype existential

relates to:

  • SR-8900 Cannot assign to a property through AnyObject

Issue Description:

Here's a protocol:

@objc protocol Flier {
    @objc optional var song2 : String {get set}
}

Now adopt Flier:

class Bird : Flier {
    @objc var song2 : String = "gobble gobble"
}

The problem is that it is impossible to set this property in a declared Flier:

var f2 : Flier = Bird()
f2.song2 = "woof" // compile error

I complained about this years ago in radar 19730251 (Xcode 6.2) but nothing has happened so I'm adding a bug here as well. As I said in my radar, to allow me to declare `song2` optional and get-set but then not to allow me to set it is incoherent at best. Ideally I should be able to set it, because why on earth not?

@mattneub
Copy link
Author

Here we are in Xcode 10 and Swift 4.2 and this has not been addressed. I realize that it must seem fairly minor, but it comes up for me year after year so I thought I'd ask again what sense this makes.

In my opinion this code should compile and work. If `f2` implements `song2`, we should be able to set it. If not, nothing happens and no harm done.

@swift-ci
Copy link
Collaborator

swift-ci commented Oct 3, 2018

Comment by Peter Kamb (JIRA)

related:

AnyObject parameter: “Cannot assign to immutable expression”
https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642

Cannot assign to a property through AnyObject
https://bugs.swift.org/browse/SR-8900

@belkadan
Copy link
Contributor

belkadan commented Jun 4, 2019

Workaround found at WWDC 2019:

let kp = \Flier.song2
f2[keyPath: kp] = "woof"

@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
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants