# KeyPath\<T, U?\> required, but I only have KeyPath\<T, U\>

**URL:** <https://forums.swift.org/t/keypath-t-u-required-but-i-only-have-keypath-t-u/15468>\
**Category:** Using Swift\
**Tags:** keypaths, optional\
**Created:** [August 24, 2018, 7:33am UTC](https://forums.swift.org/t/keypath-t-u-required-but-i-only-have-keypath-t-u/15468 "2018-08-24T07:33:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zoul/32/9559_2.png) [@zoul](https://forums.swift.org/u/zoul)\
**Post date:** [August 24, 2018, 7:33am UTC](https://forums.swift.org/t/keypath-t-u-required-but-i-only-have-keypath-t-u/15468/1 "2018-08-24T07:33:56Z")

</div>

What are my options when a `KeyPath<T, U?>` is required, but I “only” have `KeyPath<T, U>`?

```swift
struct Foo {
    let bar: String
}

let path: KeyPath<Foo, String?> = \Foo.bar // doesn’t compile

```

It’s similar with getters (when `T -> U?` is required, but I only have `T -> U`), but that can be reasonably solved with a trivial “adapter” that turns `U` into `U?`. Is there a similar reasonable shortcut for key paths, other than:

```swift
extension Foo {
    var optionalBar: String? {
        return bar
    }
}

let path: KeyPath<Foo, String?> = \Foo.optionalBar

```

Would it make sense if I could assign `KeyPath<T, U>` to `KeyPath<T, U?>` the same way I can now assign `Array<T>` into `Array<T?>`?

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [August 24, 2018, 8:14am UTC](https://forums.swift.org/t/keypath-t-u-required-but-i-only-have-keypath-t-u/15468/2 "2018-08-24T08:14:14Z")

</div>

I don't have a better answer to the question as you already showed in the second code snippet but I wonder if we could extend key-paths with functions like `mapValue` and `compactMapValue` which would operate on `Value` and transform the value to the generic type of the function.

```swift
let path = \Foo.bar
let mappedPath = path.mapValue { Optional($0) } // escaping a `Value -> Value?` closure for transformation during extraction of the value 

let value = foo[keyPath: mappedPath] // will extract `String` and then map it into `String?` with the above closure

```

@Joe_Groff what do you think?

---

<div class="post-metadata">

**Author:** ![zoul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zoul/32/9559_2.png) [@zoul](https://forums.swift.org/u/zoul)\
**Post date:** [August 24, 2018, 8:33am UTC](https://forums.swift.org/t/keypath-t-u-required-but-i-only-have-keypath-t-u/15468/3 "2018-08-24T08:33:23Z")

</div>

Yes, it would be nice for key paths to play better with other language features, [related thread](https://forums.swift.org/t/key-path-getter-promotion/11185).

---

<div class="post-metadata">

**Author:** ![rnantes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rnantes/32/5653_2.png) [@rnantes](https://forums.swift.org/u/rnantes)\
**Post date:** [May 24, 2022, 3:09am UTC](https://forums.swift.org/t/keypath-t-u-required-but-i-only-have-keypath-t-u/15468/4 "2022-05-24T03:09:16Z")

</div>

Does anybody know if there are any updates on this. It seems there is no easy way to have a function that accepts `KeyPath<Foo, String>` OR `KeyPath<Foo, String?>` as a parameter.
