You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-5667 Type inference for Swift 4 KeyPaths fails within collections
is duplicated by:
SR-8335 Cannot type check MemoryLayout.offset(of:) without specifying base type in keypath
Issue Description:
I'm trying to use PartialKeyPath as a dictionary key for one of my function arguments, but was surprised to find that the compiler said it was an ambiguous expression when I went to use the shorthand syntax. I tried to isolate it by simply using the key path as the argument and it still didn't work.
Simplified example:
structPerson{letname:Stringletage:Int}func doFancyThing(with keyPath:PartialKeyPath<Person>){}// type of expression is ambiguousdoFancyThing(with: \.age)// but the following works fineletage:PartialKeyPath<Person>= \.age
My use case:
func doFancyThing<T>(
with type:T.Type,
using keyPaths:[PartialKeyPath<T>:Any]){}// type of expression is ambiguousdoFancyThing(with:Person.self, using:[\.age:19])
The shorthand syntax works when defining variables, so I assume this is just a bug?
The text was updated successfully, but these errors were encountered:
So, it seems like it is actually failing to find the type KeyPath<Person, Int> for .age because it is failing to find binding of $T1:=Person and $T3 := Int from the context of arg conv.
For future reference as @xedin described here
A way to fix this is to use an approach similar to the way closures are type-checked in resolveClosure.
The approach will be to delay the constraint generation(now done in CSGen on visitiKeyPathExpr) until the key path type variable has a contextual type available attempting the binding.
A resolveKeyPath method would perform constraint generation and resolve the key path type together with all key path components as well as resolving the capability of the key path type (read-only, writable ... ). This makes the key path constraint not need anymore, as Pavel mentioned "The reason why key path constraint exists is related to the fact that we need to wait until all of the components are resolved to figure out a capability of the key path (read-only, writable, reference writable)" and now since resolveKeyPath will have context information we would know the capability expected, this only will need to validate that resolved vs expected at this point.
In the end, the resolveKeyPath will perform constraint generation and then resolve the keypath type in a similar way it is being done in ConstraintSystem::simplifyKeyPathConstraint.
This is just a short gist, that could be useful in the future 🙂
Additional Detail from JIRA
md5: ea2f3bb94d68bde84dac2d5e59ff60cd
duplicates:
is duplicated by:
Issue Description:
I'm trying to use PartialKeyPath as a dictionary key for one of my function arguments, but was surprised to find that the compiler said it was an ambiguous expression when I went to use the shorthand syntax. I tried to isolate it by simply using the key path as the argument and it still didn't work.
Simplified example:
My use case:
The shorthand syntax works when defining variables, so I assume this is just a bug?
The text was updated successfully, but these errors were encountered: