Once a KeyPath is type-erased to AnyKeyPath, it becomes really difficult to use it in any sort of dynamic way. (i.e. in a way where you don't have to explicitly type out the specializations for the generics of the more specialized KeyPath types.) This makes walking a collection of type-erased AnyKeyPath objects much more cumbersome than it ought to be.
Swift already creates the "most appropriate" type of KeyPath (WritableKeyPath, KeyPath, ReferenceWritableKeyPath, etc) based on the declaration of the object the KeyPath is targeting (let vs var, value vs reference type, etc.)
Why can't Swift go the opposite way automatically? Why does it have to be so painful to get back to a ReferenceWritableKeyPath from an AnyKeyPath when the AnyKeyPathalready has its rootType and valueType specified? All the stuff it needs is right there!
What stops Swift from realizing that it already has the information it needs to specialize ReferenceWritableKeyPath and does not need the developer to manually type out the generics?
The ergonomics of getting toAnyKeyPath are great. But the ergonomics of going the other way to actually use that KeyPath are...terrible. Are there any plans to change that?
You can write that line better than that, but you cannot get rid of the String?, because AnyKeyPath and PartialKeyPath don't provide their Value to the type system. nil is strongly-typed, and so I'm having trouble imagining where @bdkjones thinks it could be acquiring that.