salomonbrys
07/12/2020, 10:30 AMinterface Foo {
@Throws(CancellationException::class, MyException::class)
suspend fun bar(param: Int): String
}
This interface gets translated thusly in Swift:
public protocol Foo {
/**
@note This method converts instances of CancellationException, MyException to errors.
Other uncaught Kotlin exceptions are fatal.
*/
func bar(param: Int32, completionHandler: @escaping (String?, Error?) -> Void)
}
I can (and do) implement this interface in Swift, creating a class with coroutine functions that I can use in Kotlin (btw, blown away by the fact that it actually works, amazing job!).
What I don't see, however, is how can the bar
function use the completionHandler
with a MyException
. In other words, I need to express a async failure in Kotlin. In other other words, the bar function may fail asynchronously, and I need the coroutine (not the callback) to throw a MyException
:
class SwiftFoo : Foo {
func bar(param: Int32, completionHandler: @escaping (TCPSocket?, Error?) -> Void) {
someSwiftAsyncApi(param) { result in
if let result = result { completionHandler(result, nil) }
else { completionHandler(nil, MyException("someSwiftAsyncApi failed!")) } // TYPE ERROR
}
}
}
This does not compile because MyException
is not a Swift Error
.
Any way to do this?