Hey guys, i have a question, how do we handle Eith...
# multiplatform
s
Hey guys, i have a question, how do we handle Either response in iOS for KMM Like if it's Either< Type_A , Type_B > How do i get the values of Type_A and Type_B ?
s
I ended up writing an enum in swift to represent the Either type and a util to convert from the Kotlin one to the Swift one It wasn't particularly pretty but works 🤷
a
The Swift has
*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.
s
Can you give me a sample of how that util class looks like ?
I tried the result one, but i am not able to access it
Sorry, i am a complete noob in iOS
a
Could you please describe you situation a bit more clear? Are you want to pass Either type from KMM library to swift and convert it there to some native type?
s
ok so in my KMM module I have a Either class where it can either be a data class if it's success and String class if it's a failure, which i am exposing, in android i can do it easily but in iOS i am having problems on accessing the inner data classes
a
Okay, i see. If you could not access some class from swift, it means it wasn’t propagated into framework header file. It could be done it 2 ways: tell compile to import all classes from particular module or do simple hack (pit it somewhere inside iosMain module on Kotlin side)
Copy code
fun 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:
Copy code
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>
w
I assume you have a sealed class?
Copy code
sealed 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:
Copy code
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
}
s
Copy code
sealed 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)
        }
    }
}
I have it like this
@Andrei Salavei ok, i will try it out