matt
matt

Reputation: 535989

Swift struct adopting protocol with static read-write property doesn't conform?

Why doesn't this compile in Swift 1.2?

protocol Proto {
    static var name : String {get set}
}
struct Struct : Proto {
    static var name : String = "name"
}

(In Swift 1.1, just substitute class for static inside the protocol declaration. Same problem.)

The compiler complains that I'm not conforming to the protocol. But why am I not? It's easy to prove that the static property name in Struct is both readable and writable, so I've satisfied the spirit of the protocol, surely.

I have some additional observations:

But I'm no closer to understanding what the compiler doesn't like about what I've got. Why doesn't a static stored property satisfy the protocol requirement?

Upvotes: 2

Views: 6558

Answers (2)

Chris Conover
Chris Conover

Reputation: 9039

The mothership fixeth in 6.3 (6D543q):

protocol Proto {
    static var name : String {get set}
}

struct Struct : Proto {
    static var name : String = "name"
}

Struct.name = "Frodo"

println(Struct.name)

now works:

-> "Frodo"

(tested in Playgrounds) It really does seem like static was treated as a let / const variable, but your case now works in 6.3 Beta 3. I'm just happy that lldb symbols are unbroken.

Upvotes: 4

matt
matt

Reputation: 535989

At this point I'm persuaded by Nate Cook's example that this is nothing but a bug in the Swift compiler. As he points out, merely adding an empty didSet observer on the static variable allows the code to compile. The fact that this could make a difference, even though it makes no functional difference, has "bug" written all over it.

Upvotes: 3

Related Questions