Array of PartialKeyPath<T> ambiguous?


(Joanna Carter) #1

I want to return an array of PartialKeyPaths but the compiler tells me I can't use the "shorthand" property names as they are ambiguous:

  public static var keyPaths: [PartialKeyPath<Person>]
  {
    return [\.name, \.postcode] // error : Type of expression is ambiguous without more context
  }

This sort of feels like a covariance issue on the Value parameter in PartialKeyPath but I'm not sure why it should raise an ambiguity.

Of course I could return an array of AnyKeyPath but I want to avoid being able to return keyPaths that don't belong to the same root type


(Jordan Rose) #2

You're completely right; this is SR-5667.

EDIT: You can work around it by including the type name: [\Person.name, \Person.postcode].


(Joanna Carter) #3

Ok thanks. Any idea of a timeframe for the fix? Unfortunately, I'm only too aware of the workaround :wink:

There's also a question of, possibly, unhelpful error messages:

  public static var keyPaths: [PartialKeyPath<Person>]
  {
    return [\Person.name, \Fred.handle] // error : Type 'Person' has no member 'handle'
  }

Surely this should be something like "Cannot add a PartialKeyPath<Fred> to an array of PartialKeyPath<Person>"

And:

  public static var keyPaths: [AnyKeyPath]
  {
    return [\Person.name, \Person.handle] // error : Type of expression is ambiguous without more context
  }

Instead, shouldn't this give "Type 'Person' has no member 'handle'"?

Of course:

  public static var keyPaths: [PartialKeyPath<Person>]
  {
    return [\Person.name, \Person.handle] // error : Type 'Person' has no member 'handle'
  }

Does give the correct message.


(Jordan Rose) #4

Yep, both of these error messages need to be improved, and deserve their own tracking bugs.


(Joe Groff) #5

There are some bug fixes to key path type checking that are in the master branch that IIRC did not make it into Swift 4.2. If you're up to trying out a nightly snapshot, you might see whether things are improved there.