I recently ran into a compiler error that surprised me. Here's some code that demonstrates the issue (using Xcode 14 beta 5):
struct Test: Identifiable {
let id: Int
}
extension Test.ID {
static var current: Self { 0 }
}
print(Test.ID.current) // Type 'Test.ID' (aka 'Int') has no member 'current'
The compiler has inferred that type Test.ID
is Int
, but can't find the static value. Making the associated type explicit eliminates the error:
struct Test {
let id: Int
}
extension Test: Identifiable {
typealias ID = Int
}
extension Test.ID {
static var current: Self { 0 }
}
print(Test.ID.current)
It also works if you extend Int
instead of Test.ID
to declare the static property. Obviously this is easy to get around, but am I wrong to assume the original example should be valid?