Open
Description
Method returning throws -> Never
should be callable as throw method()
, not try method()
.
This is not the case right now:
struct Err: Error { }
func logThenThrow() throws -> Never {
...
throw Err()
}
throw logThenThrow() /// Error: Call can throw but is not marked with 'try'
func saveInDBThenThrow() async throws -> Never {
...
throw Err()
}
throw await saveInDBThenThrow() /// Error: Call can throw but is not marked with 'try'
Motivation
This is useful in situations where you want to perform some work and only then throw.
Is is still possible right now using the try
keyword instead of throw
, but that syntax is not as clear as it should be on call-sites.
Solution
If a method returns Never
but is still declared as throws, such as throws -> Never
, swift compiler should require users to use the throw
keyword behind the method name, instead of try
.
Alternatives considered