Appending KeyPaths to Optional values; impossible?

I've run into what I think is a limitation of KeyPaths that I think shouldn't be a limitation.

Consider the following:

struct Thing {
  var item: NestedItem?
}

struct NestedItem {
  var name: String
}

let rootKeyPath = \Thing.item
let nameKeyPath = \NestedItem.name

// let appended = rootKeyPath.appending(path: nameKeyPath)

If my observations are correct, there is no way to append a KeyPath<NestedItem, String> onto a KeyPath<Thing, NestedItem?>.

Am I correct here? Is this a bug? An unimplemented edge case? A purposefully omitted impossibility?

Thanks!

2 Likes

Been wondering the same but forgot to ever ask. If it was implemented, I'd also expect the equality to hold that (\Thing.item).appending(\NestedItem.name) == \Thing.item?.name.

1 Like
Terms of Service

Privacy Policy

Cookie Policy