Extended idea on typed-throws: automatic determination

Assuming a syntax like:

func myFunction() throws(MyErrorType)

where a non-throwing function is the same as throwing Never and a general-throwing function is the same as throwing Swift.Error, can we add having a setting where the compiler figures out the appropriate type? Fortunately, we already have another part of the syntax where the compiler figures out the exact type statically: opaque types! So let's adapt that:

func myFunction2() throws(some Swift.Error)

If all the throwing code within the function resolves to the same type, then that type will be used. If there are multiple thrown types, the most derived common class (that conforms to Error) will be used. If there is no shared class, including when at least one thrown type is a struct or enum, then the Error existential type is used (since it self-conforms). The error type chosen has to conform to the protocol within the specified opaque type (which has to either be or refine Swift.Error); otherwise flag a compile-time error.

4 Likes