PartialKeyPath ambiguity without root type

struct Person
{
  var name: String = ""
  
  var age: Int = 0
}

If I do the following switch statement, everything is fine.

    let keyPath: PartialKeyPath<Person> = \.name
    
    switch keyPath
    {
      case \Person.name:
        print("name")
      case \Person.age:
        print("age")
      default:
        print("default")
    }

… but, even though the keyPath's root type is explicitly declared, if I omit the root type from the cases :

    let keyPath: PartialKeyPath<Person> = \.name
    
    switch keyPath
    {
      case \.name:
        print("name")
      case \.age:
        print("age")
      default:
        print("default")
    }

I get the error : Type of expression is ambiguous without more context

Is this a bug or can someone please explain why it doesn't work as I am expecting?

Seems related to my thread:

Yup, that's the one :nerd_face:

Terms of Service

Privacy Policy

Cookie Policy