withCheckedThrowingContinuation and typed Errors

First, read Where is FullTypedThrows? - #15 by hborla.


You can't actually create a Continuation of any kind, with anything but Never or any Error. This has not changed with Xcode 16 Beta 1. However, you can at least mask this outside of the Model type, either with an overload, a do-catch statement, or a function that can apply that statement.

func doOperation() async throws(OperationFailed) {
  try await forceError(OperationFailed.self) {
    try await withCheckedThrowingContinuation { continuation in
      operationContinuation = continuation
    }
  }
}
func forceError<Success, Error: Swift.Error>(
  _: Error.Type,
  _ body: () async throws -> Success
) async throws(Error) -> Success {
  do {
    return try await body()
  } catch {
    throw error as! Error
  }
}
2 Likes