# Throwable accessors

**URL:** <https://forums.swift.org/t/throwable-accessors/20509>\
**Category:** Compiler\
**Tags:** compiler, throw\
**Created:** [February 14, 2019, 9:28pm UTC](https://forums.swift.org/t/throwable-accessors/20509 "2019-02-14T21:28:19Z")\
**Posts on this page:** 1\
**Showing post:** 49

<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:** [March 25, 2019, 9:40am UTC](https://forums.swift.org/t/throwable-accessors/20509/49 "2019-03-25T09:40:06Z")

</div>

How would key-paths work on throwable accessors?

```swift
struct S {
  var a: Int { get throws { return 42 } }

  var b: Int { return 42 }
}

```

I cannot solve this problem in my head without new key-path classes and typed throws where when `throws` is omitted it equals `throws(Never)`.

```swift
func extract<Root, Value, Path, Error>(
  _ path: Path,
  from root: Root 
) throws(Error) -> Value
  where 
  Path: KeyPath<Root, Value>,
  Error: Swift.Error
{
  return try root[keyPath: path]
}

```

Usage:

```swift
let s = S()

_ = try! extract(\.a, from: s) // extract<S, Int, ??, Swift.Error>
_ = extract(\.b, from: s) // extract<S, Int, KeyPath<S, Int>, Never>

```

But new key-path classes would require new OS 😢 even though the other features are potentially backwards compatible.

cc @Joe_Groff

Can we potentially retroactively introduce a generic `Error` type parameter to `KeyPath<..., Error>` and it's subclasses?

---

_[View the full topic](https://forums.swift.org/t/throwable-accessors/20509)._
