Srijit Saha
11/17/2022, 4:38 AMSam
11/17/2022, 7:35 AMAndrei Salavei
11/17/2022, 9:06 AM*enum* Result<Success, Failure> *where* Failure : Error
type that can be helpful, you can adopt it, or elaborate your situation a bit to find other solutions.Srijit Saha
11/17/2022, 9:08 AMSrijit Saha
11/17/2022, 9:08 AMSrijit Saha
11/17/2022, 9:09 AMAndrei Salavei
11/17/2022, 9:21 AMSrijit Saha
11/17/2022, 9:23 AMAndrei Salavei
11/17/2022, 9:53 AMfun Either.Value<*>.foo() {}
fun Either.Error<*>.foo() {}
fun Either<*>.foo() {}
After that it just a technical question how you want to handle it is Swift:
func handle<ValueType>(result: Either<ValueType>) {
switch either {
case let value as EitherValue<ValueType>:
print(value.value!)
case let error as EitherError<ValueType>:
print(error.message)
default:
fatalError("Unexpected type")
}
}
Note that Either type can be named a bit differently after KMM interop. In our case it called Platform_apiEither<T>
and Platform_apiEitherValue<ValueType>
and Platform_apiEitherError<ValueType>
Werner Altewischer
11/17/2022, 10:30 AMsealed class Either<T: Any> {
class EitherValue<T: Any>(val value: T): Either<T>()
class EitherError<T: Any>(val error: Throwable): Either<T>()
}
In Swift you can handle that as follows:
let either: Either<ValueType> = ...
if let eitherValue = either as? EitherValue<ValueType> {
// code for value
} else if let eitherError = either as? EitherError<ValueType> {
// code for error
}
Srijit Saha
11/17/2022, 10:31 AMsealed class Either<out S, out T> {
data class Fail<out S> internal constructor(val failureCause: S) : Either<S, Nothing>() {
companion object {
operator fun <S> invoke(f: S): Either<S, Nothing> = Fail(f)
}
}
data class Success<out T> internal constructor(val successResponse: T) : Either<Nothing, T>() {
companion object {
operator fun <T> invoke(s: T): Either<Nothing, T> = Success(s)
}
}
}
Srijit Saha
11/17/2022, 10:31 AMSrijit Saha
11/17/2022, 10:32 AM