<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">It’s funny, I literally just came across this. Turns out this is what the Dispatch overlay uses for dispatch_sync/DispatchQueue.sync.</div><div class=""><br class=""></div><div class="">Here’s an even shorter example:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Courier" class="">func throwsUnexpected(one: ()throws-&gt;Void, hack: (Error)throws-&gt;Void) rethrows {</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; try hack(SomeUnexpectedError.boo)</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">func hackedRethrow(func: ()throws-&gt;Void) rethrows {</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; try throwsUnexpected(one: func, hack: { throw $0 })</font></div><div class=""><font face="Courier" class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">The compiler allows this. Even though hackedRethrow says it rethrows the error from the closure, it calls in to another closure which, to its credit, does rethrow — albeit errors from the wrong closure!</div><div class=""><br class=""></div><div class="">It’s a handy hack, so if it was removed we’d need some way to instruct the compiler “even though you can’t prove it, I promise this function only ever rethrows errors from the closure”. There are legitimate use-cases for this (such as the aforementioned DispatchQueue.sync)</div><div class=""><br class=""></div><div class="">- Karl</div><br class=""><div><blockquote type="cite" class=""><div class="">On 23 Feb 2017, at 19:09, Matthew Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">I put together some valid Swift 3 sample code in case anyone is having trouble understanding the discussion of rethrows. &nbsp;The behavior may not be immediately obvious.<br class=""><br class="">func ithrow() throws { throw E.e }<br class="">func nothrow() {}<br class=""><br class="">func rethrower(f: () throws -&gt; Void, g: () throws -&gt; Void) rethrows {<br class=""> &nbsp;&nbsp;&nbsp;do {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try f()<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// I am not allowed to call `ithrow` here because it is not an argument<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// and a throwing catch clause is reachable if it throws.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// This is because in a given invocation `f` might not throw but `ithrow` does.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Allowing the catch clause to throw an error in that circumstance violates the<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// invariant of `rethrows`.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// try ithrow()<br class=""> &nbsp;&nbsp;&nbsp;} catch _ as E {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// I am allowed to catch an error if one is dynamically thrown by an argument.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// At this point I am allowed to throw *any* error I wish.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// The error I rethrow is not restricted in any way at all.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// That *does not*<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw F.f<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;&nbsp;&nbsp;do {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Here I am allowed to call `ithrow` because the error is handled.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// There is no chance that `rethrower` throws evne if `ithrow` does.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try ithrow()<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// We handle any error thrown by `g` internally and don't propegate it.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// If `f` is a non-throwing function `rethrower` should be considered non-throwing<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// regardless of whether `g` can throw or not because if `g` throws the error is handled.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Unfortunately `rethrows` is not able to handle this use case.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// We need to treat all functions with an uninhabitable errror type as non-throwing<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// if we want to cover this use case.<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try g()<br class=""> &nbsp;&nbsp;&nbsp;} catch _ {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("The error was handled internally")<br class=""> &nbsp;&nbsp;&nbsp;}<br class="">}<br class=""><br class="">// `try` is obviously required here.<br class="">try rethrower(f: ithrow, g: ithrow)<br class=""><br class="">// `try` is obviously not required here.<br class="">// This is the case `rethrows` can handle correctly: *all* the arguments are non-throwing.<br class="">rethrower(f: nothrow, g: nothrow)<br class=""><br class="">// ok: `f` can throw so this call can as well.<br class="">try rethrower(f: ithrow, g: nothrow)<br class=""><br class="">// I should be able to remove `try` here because any error thrown by `g` is handled internally<br class="">// by `rethrower` and is not propegated.<br class="">// If we treat all functions with an uninhabitable error type as non-throwing it becomes possible<br class="">// to handle this case when all we're doing is propegating errors that were thrown.<br class="">// This is because in this example we would only be propegating an error thrown by `f` and thus<br class="">// we would be have an uninhabitable error type.<br class="">// This is stil true if you add additional throwing arguments and propegate errors from<br class="">// several of them using a sum type.<br class="">// In that case we might have an error type such as Either&lt;AnUninhabitableType, AnotherUninhabitableType&gt;.<br class="">// Because all cases of the sum type have an associated value with an uninhabitable the sum type is as well.<br class="">try rethrower(f: nothrow, g: ithrow)<br class=""><br class=""><blockquote type="cite" class="">On Feb 22, 2017, at 6:37 PM, Matthew Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""># Analysis of the design of typed throws<br class=""><br class="">## Problem<br class=""><br class="">There is a problem with how the proposal specifies `rethrows` for functions that take more than one throwing function. &nbsp;The proposal says that the rethrown type must be a common supertype of the type thrown by all of the functions it accepts. &nbsp;This makes some intuitive sense because this is a necessary bound if the rethrowing function lets errors propegate automatically - the rethrown type must be a supertype of all of the automatically propegated errors.<br class=""><br class="">This is not how `rethrows` actually works though. &nbsp;`rethrows` currently allows throwing any error type you want, but only in a catch block that covers a call to an argument that actually does throw and *does not* cover a call to a throwing function that is not an argument. &nbsp;The generalization of this to typed throws is that you can rethrow any type you want to, but only in a catch block that meets this rule.<br class=""><br class=""><br class="">## Example typed rethrow that should be valid and isn't with this proposal<br class=""><br class="">This is a good thing, because for many error types `E` and `F` the only common supertype is `Error`. &nbsp;In a non-generic function it would be possible to create a marker protocol and conform both types and specify that as a common supertype. &nbsp;But in generic code this is not possible. &nbsp;The only common supertype we know about is `Error`. &nbsp;The ability to catch the generic errors and wrap them in a sum type is crucial.<br class=""><br class="">I'm going to try to use a somewhat realistic example of a generic function that takes two throwing functions that needs to be valid (and is valid under a direct generalization of the current rules applied by `rethrows`).<br class=""><br class="">enum TransformAndAccumulateError&lt;E, F&gt; {<br class=""> &nbsp;case transformError(E)<br class=""> &nbsp;case accumulateError(F)<br class="">}<br class=""><br class="">func transformAndAccumulate&lt;E, F, T, U, V&gt;(<br class=""> &nbsp;_ values: [T], <br class=""> &nbsp;_ seed: V,<br class=""> &nbsp;_ transform: T -&gt; throws(E) U, <br class=""> &nbsp;_ accumulate: throws (V, U) -&gt; V<br class="">) rethrows(TransformAndAccumulateError&lt;E, F&gt;) -&gt; V {<br class=""> &nbsp;var accumulator = seed<br class=""> &nbsp;try {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;for value in values {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accumulator = try accumulate(accumulator, transform(value))<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;} catch let e as E {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;throw .transformError(e)<br class=""> &nbsp;} catch let f as F {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;throw .accumulateError(f)<br class=""> &nbsp;}<br class=""> &nbsp;return accumulator<br class="">}<br class=""><br class="">It doesn't matter to the caller that your error type is not a supertype of `E` and `F`. &nbsp;All that matters is that the caller knows that you don't throw an error if the arguments don't throw (not only if the arguments *could* throw, but that one of the arguments actually *did* throw). &nbsp;This is what rethrows specifies. &nbsp;The type that is thrown is unimportant and allowed to be anything the rethrowing function (`transformAndAccumulate` in this case) wishes.<br class=""><br class=""><br class="">## Eliminating rethrows<br class=""><br class="">We have discussed eliminating `rethrows` in favor of saying that non-throwing functions have an implicit error type of `Never`. &nbsp;As you can see by the rules above, if the arguments provided have an error type of `Never` the catch blocks are unreachable so we know that the function does not throw. &nbsp;Unfortunately a definition of nonthrowing functions as functions with an error type of `Never` turns out to be too narrow.<br class=""><br class="">If you look at the previous example you will see that the only way to propegate error type information in a generic function that rethrows errors from two arguments with unconstrained error types is to catch the errors and wrap them with an enum. &nbsp;Now imagine both arguments happen to be non-throwing (i.e. they throw `Never`). &nbsp;When we wrap the two possible thrown values `Never` we get a type of `TransformAndAccumulateError&lt;Never, Never&gt;`. &nbsp;This type is uninhabitable, but is quite obviously not `Never`. <br class=""><br class="">In this proposal we need to specify what qualifies as a non-throwing function. &nbsp;I think we should specifty this in the way that allows us to eliminate `rethrows` from the language. &nbsp;In order to eliminate `rethrows` we need to say that any function throwing an error type that is uninhabitable is non-throwing. &nbsp;I suggest making this change in the proposal.<br class=""><br class="">If we specify that any function that throws an uninhabitable type is a non-throwing function then we don't need rethrows. &nbsp;Functions declared without `throws` still get the implicit error type of `Never` but other uninhabitable error types are also considered non-throwing. &nbsp;This provides the same guarantee as `rethrows` does today: if a function simply propegates the errors of its arguments (implicitly or by manual wrapping) and all arguments have `Never` as their error type the function is able to preserve the uninhabitable nature of the wrapped errors and is therefore known to not throw.<br class=""><br class="">### Why this solution is better<br class=""><br class="">There is one use case that this solution can handle properly that `rethrows` cannot. &nbsp;This is because `rethrows` cannot see the implementation so it must assume that if any of the arguments throw the function itself can throw. &nbsp;This is a consequence of not being able to see the implementation and not knowing whether the errors thrown from one of the functions might be handled internally. &nbsp;It could be worked around with an additional argument annotation `@handled` or something similar, but that is getting clunky and adding special case features to the language. &nbsp;It is much better to remove the special feature of `rethrows` and adopt a solution that can handle edge cases like this.<br class=""><br class="">Here's an example that `rethrows` can't handle:<br class=""><br class="">func takesTwo&lt;E, F&gt;(_ e: () throws(E) -&gt; Void, _ f: () throws(F) -&gt; Void) throws(E) -&gt; Void {<br class=""> try e()<br class=""> do {<br class=""> &nbsp;&nbsp;try f()<br class=""> } catch _ {<br class=""> &nbsp;&nbsp;print("I'm swallowing f's error")<br class=""> }<br class="">}<br class=""><br class="">// Should not require a `try` but does in the `rethrows` system.<br class="">takesTwo({}, { throw MyError() })<br class=""><br class="">When this function is called and `e` does not throw, rethrows will still consider `takesTwo` a throwing function because one of its arguments throws. &nbsp;By considering all functions that throw an uninhabited type to be non-throwing, if `e` is non-throwing (has an uninhabited error type) then `takesTwo` is also non-throwing even if `f` throws on every invocation. &nbsp;The error is handled internally and should not cause `takesTwo` to be a throwing function when called with these arguments.<br class=""><br class="">## Error propegation<br class=""><br class="">I used a generic function in the above example but the demonstration of the behavior of `rethrows` and how it requires manual error propegation when there is more than one unbounded error type involved if you want to preserve type information is all relevant in a non-generic context. &nbsp;You can replace the generic error types in the above example with hard coded error types such as `enum TransformError: Error` and `enum AccumulateError: Error` in the above example and you will still have to write the exact same manual code to propegate the error. &nbsp;This is the case any time the only common supertype is `Error`.<br class=""><br class="">Before we go further, it's worth considering why propegating the type information is important. &nbsp;The primary reason is that rethrowing functions do not introduce *new* error dependencies into calling code. &nbsp;The errors that are thrown are not thrown by dependencies of the rethrowing function that we would rather keep hidden from callers. &nbsp;In fact, the errors are not really thrown by the rethrowing function at all, they are only propegated. &nbsp;They originate in a function that is specified by the caller and upon which the caller therefore already depends. &nbsp;<br class=""><br class="">In fact, unless the rethrowing function has unusual semantics the caller is likely to expect to be able catch any errors thrown by the arguments it provides in a typed fashion. &nbsp;In order to allow this, a rethrowing function that takes more than one throwing argument must preserve error type information by injecting it into a sum type. &nbsp;The only way to do this is to catch it and wrap it as can be seen in the example above.<br class=""><br class="">### Factoring out some of the propegation boilerplate<br class=""><br class="">There is a pattern we can follow to move the boilerplate out of our (re)throwing functions and share it between them were relevant. &nbsp;This keeps the control flow in (re)throwing functions more managable while allowing us to convert errors during propegation. &nbsp;This pattern involves adding an overload of a global name for each conversion we require:<br class=""><br class="">func propegate&lt;E, F, T&gt;(@autoclosure f: () throws(E) -&gt; T) <br class=""> &nbsp;&nbsp;&nbsp;&nbsp;rethrows(TransformAndAccumulateError&lt;E, F&gt;) -&gt; T &nbsp;{<br class=""> do {<br class=""> &nbsp;&nbsp;try f()<br class=""> } catch let e {<br class=""> &nbsp;&nbsp;throw .transformError(e)<br class=""> }<br class="">}<br class="">func propegate&lt;E, F, T&gt;(@autoclosure f: () throws(F) -&gt; T) <br class=""> &nbsp;&nbsp;&nbsp;&nbsp;rethrows(TransformAndAccumulateError&lt;E, F&gt;) -&gt; T &nbsp;{<br class=""> do {<br class=""> &nbsp;&nbsp;try f()<br class=""> } catch let e {<br class=""> &nbsp;&nbsp;throw .accumulateError(e)<br class=""> }<br class="">}<br class=""><br class="">Each of these overloads selects a different case based on the type of the error that `f` throws. &nbsp;The way this works is by using return type inference which can see the error type the caller has specified. &nbsp;The types used in these examples are intentionally domain specific, but `TransformAndAccumulateError` could be replaced with generic types like `Either` for cases when a rethrowing function is simply propegating errors provided by its arguments.<br class=""><br class="">### Abstraction of the pattern is not possible<br class=""><br class="">It is clear that there is a pattern here but unforuntately we are not able to abstract it in Swift as it exists today.<br class=""><br class="">func propegate&lt;E, F, T&gt;(@autoclosure f: () throws(E) -&gt; T) rethrows(F) -&gt; T <br class=""> &nbsp;where F: ??? initializable with E ??? {<br class=""> &nbsp;do {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;try f()<br class=""> &nbsp;} catch let e {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;throw // turn e into f somehow: F(e) ???<br class=""> &nbsp;}<br class="">}<br class=""><br class="">### The pattern is still cumbersome<br class=""><br class="">Even if we could abstract it, this mechanism of explicit propegation is still a bit cumbersome. &nbsp;It clutters our code without adding any clarity.<br class=""><br class="">for value in values {<br class=""> let transformed = try propegate(try transform(value))<br class=""> accumulator = try propegate(try accumulate(accumulator, transformed))<br class="">}<br class=""><br class="">Instead of a single statement and `try` we have to use one statement per error propegation along with 4 `try` and 2 `propegate`.<br class=""><br class="">For contrast, consider how much more concise the original version was:<br class=""><br class="">for value in values {<br class=""> accumulator = try accumulate(accumulator, transform(value))<br class="">}<br class=""><br class="">Decide for yourself which is easier to read.<br class=""><br class="">### Language support<br class=""><br class="">This appears to be a problem in search of a language solution. &nbsp;We need a way to transform one error type into another error type when they do not have a common supertype without cluttering our code and writing boilerplate propegation functions. &nbsp;Ideally all we would need to do is declare the appropriate converting initializers and everything would fall into place.<br class=""><br class="">One major motivating reason for making error conversion more ergonomic is that we want to discourage users from simply propegating an error type thrown by a dependency. &nbsp;We want to encourage careful consideration of the type that is exposed whether that be `Error` or something more specific. &nbsp;If conversion is cumbersome many people who want to use typed errors will resort to just exposing the error type of the dependency.<br class=""><br class="">The problem of converting one type to another unrelated type (i.e. without a supertype relationship) is a general one. &nbsp;It would be nice if the syntactic solution was general such that it could be taken advantage of in other contexts should we ever have other uses for implicit non-supertype conversions.<br class=""><br class="">The most immediate solution that comes to mind is to have a special initializer attribute `@implicit init(_ other: Other)`. &nbsp;A type would provide one implicit initializer for each implicit conversion it supports. &nbsp;We also allow enum cases to be declared `@implicit`. &nbsp;This makes the propegation in the previous example as simple as adding the `@implicit ` attribute to the cases of our enum:<br class=""><br class="">enum TransformAndAccumulateError&lt;E, F&gt; {<br class=""> &nbsp;@implicit case transformError(E)<br class=""> &nbsp;@implicit case accumulateError(F)<br class="">}<br class=""><br class="">It is important to note that these implicit conversions *would not* be in effect throughout the program. &nbsp;They would only be used in very specific semantic contexts, the first of which would be error propegation.<br class=""><br class="">An error propegation mechanism like this is additive to the original proposal so it could be introduced later. &nbsp;However, if we believe that simply passing on the error type of a dependency is often an anti-pattern and it should be discouraged, it is a good idea to strongly consider introducing this feature along with the intial proposal.<br class=""><br class=""><br class="">## Appendix: Unions<br class=""><br class="">If we had union types in Swift we could specify `rethrows(E | F)`, which in the case of two `Never` types is `Never | Never` which is simply `Never`. &nbsp;We get rethrows (and implicit propegation by subtyping) for free. &nbsp;Union types have been explicitly rejected for Swift with special emphasis placed on both generic code *and* error propegation. &nbsp;<br class=""><br class="">In the specific case of rethrowing implicit propegation to this common supertype and coalescing of a union of `Never` is very useful. &nbsp;It would allow easy propegation, preservation of type information, and coalescing of many `Never`s into a single `Never` enabling the simple defintion of nonthrowing function as those specified to throw `Never` without *needing* to consider functions throwing other uninhabitable types as non-throwing (although that might still be a good idea).<br class=""><br class="">Useful as they may be in this case where we are only propegating errors that the caller already depends on, the ease with which this enables preservation of type information encourages propegating excess type information about the errors of dependencies of a function that its callers *do not* already depend on. &nbsp;This increases coupling in a way that should be considered very carefully. &nbsp;Chris Lattner stated in the thread regarding this proposal that one of the reasons he opposes unions is because they make it too easy too introduce this kind of coupling carelessly.<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></body></html>