Created
September 26, 2024 18:25
-
-
Save robertmryan/feaa23876da000e681d41b81031fb33b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum MyError: Error { | |
case one | |
case two | |
} | |
actor Example { | |
func funcWithTypedThrow() async throws(MyError) { throw .one } | |
// OK | |
func foo() async { | |
// closures can be defined as throwing a particular error type | |
let closure: () async throws(MyError) -> Void = { try await self.funcWithTypedThrow() } | |
// thus, this is OK | |
do { | |
try await closure() | |
} catch { | |
switch error { | |
case .one: print("one") | |
case .two: print("two") | |
} | |
} | |
} | |
// Not OK | |
func bar() async { | |
// but closures do not (yet) infer the typed-throw type | |
let closure = { try await self.funcWithTypedThrow() } | |
// thus, this will produce the following compile-time errors | |
do { | |
try await closure() | |
} catch { | |
switch error { | |
case .one: print("one") // Error: Type '_ErrorCodeProtocol' has no member 'one' | |
case .two: print("two") // Error: Type '_ErrorCodeProtocol' has no member 'two' | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suspect this is all related to #74555.
Also, FWIW, SE-0413 seems to suggest that typed throw type inference is a work-in-progress: