[swift-evolution] [Proposal] Typed throws
Anton Zhilin
antonyzhilin at gmail.com
Wed Feb 22 12:15:15 CST 2017
I understand how parametric polymorphism works *in Haskell*. But we talk
about Swift, and there *is* a way to get an instance of E. I’ll explain it
another way:
func bypassRethrows<E: Error>(_ f: () throws(E) -> ()) throws(E) {
let error: Error = MyError() // create an instance of `MyError`
if MyError.self is E.Type { // in case `E` happens to be `MyError`
let e: E = error as! E // then we've actually created an
instance of `E`, and we can downcast safely
throw e // voila, acquired an instance of `E`
}
}
let f: () throws MyError -> () = { }
try bypassRethrows(f) // actually throws `MyError`,
without ever calling `f`
What line here seems impossible?
2017-02-22 18:39 GMT+03:00 Matthew Johnson <matthew at anandabits.com>:
No because there are many types that conform to `Error` but are not `E` and
> the signature says you only throw `E`. You have no way to get an instance
> of `E` except by catching one thrown by `f` because `Error` does not have
> any initializers and you don’t have any visibility to anything that
> provides an `E` in any way except when `f` throws.
>
> I am hoping to have time to write up my analysis of generic rethrowing
> functions later today. There are some very interesting tradeoffs we need
> to make.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170222/afd6ae62/attachment.html>
More information about the swift-evolution
mailing list