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 <[email protected]>:
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.
>
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution