Reputation: 535989
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:
If I remove the set
from the protocol requirement, the issue goes away.
If I leave the set
in place, but I remove the static
(or class
) from the protocol requirement and the static
from the Struct implementation, the issue goes away.
If I leave the static
in place and turn the stored variable into a computed variable, the issue goes away.
If I change the struct to a class, the issue goes away.
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
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
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