How would key-paths work on throwable accessors?
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)
.
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:
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?