I have a partial keypath and I would like to use a switch on it.
Question:
Why is the switch statement not exhaustive, wouldn't the compiler know about all the possible properties of Car?. Am I missing something?
Is there a way to do this without adding a default case or a better approach?
Also noticed that in the case I had to mention \Car.price instead of \.price, I thought compiler would be able to infer. Am I missing something?
Code:
struct Car {
var name : String
var price : Double
}
func f1(partialKeyPath: PartialKeyPath<Car>) {
switch partialKeyPath { //Error: Switch must be exhaustive
case \Car.price:
print("price")
case \Car.name:
print("name")
}
}
Yes, you can form a valid keypath to those declared in extension.
The only way for it to be provable is to have Car in a very strict scope, e.g, private (I don’t know if there’s such checking). Even then, if Car conforms to public protocols, you could still form key paths to extensions of those protocols.
Protocol seems interesting, I didn't think that far. I tried with a protocol keypath still not exhaustive. I guess protocols could be extended too ... :D
Not sure why the root type in the case couldn't be inferred.
Keep in mind that I’m not saying there’s any exhaustive proof in this area. I’m only saying that, if there’s one, only those meeting these conditions (is non-public, and not conforming to public protocols like Equatable) would be eligible.