In most of the cases the types I use in subscripts were already Hashable
so I never bumped into this error, and when I bumped into it I could fix it as it was okay to make the types conform to Hashable
.
- However I would like to understand why this restriction exists?
I know that a key-path itself is Hashable
which lets me assume that every intermediate path must be constructed from something that is also Hashable
. From a different view angle however, a subscript is similar to a method and it's a future direction to allow constructing key-paths for methods as well.
-
Would this mean that any method parameter type would also be required to be
Hashable
?
This seems like it could cause a bigger issue. -
If the answer to previous question is 'no', then why are subscripts parameter types required to conform to
Hashable
today?
class Key {
init() {}
}
struct S {
subscript(key: Key) -> Int {
return 42
}
}
let s = S()
\S.[Key()].description // error: Subscript index of type 'Key' in a key path must be Hashable
cc @Joe_Groff
The restriction is mentioned in the original proposal, buy can you elaborate why this the case and what would it mean for key-paths for methods?
Forming a key path through subscripts (e.g. Array / Dictionary) will have the limitation that the parameter's type(s) must be
Hashable
. Should the archival and serialization proposal be accepted, we would also like to includeCodable
with an eye towards being able to make key pathsCodable
themselves in the future.