Hi! We use our common module for communicating wit...
# kotlin-native
d
Hi! We use our common module for communicating with an API and, as everyone else, use a sealed class for the response:
Copy code
sealed class Response<out T : Any> {
    data class Success<out T : Any>(val data: T) : Response<T>()
    data class Error(val reason: ErrorReason) : Response<Nothing>()
}
Receiving success in ios is ok. However, we are not able to cast the result to an
Response.Error
. When printing the object to console (
(lldb) e dump(response)
) we can see that it is an
Optional(Error)
object with all the params we are looking for, but we cannot cast it to en Error in any way. Anyone knows what we have to do?
@louiscad, you are active here and seem to know a lot 🙂 Do you have any idea what to do?
l
Option(Error)
looks like the
NSError?
type, which cannot be cast to your custom type
@dambakk Can you show the definition of
ErrorReason
?
d
Yeah, its another sealed class defining our specific error cases or fall backs to an Exception:
Copy code
sealed class ErrorReason(override val message: String) : Throwable(message) {
    object TokenExpired : ErrorReason("Token expired")
    object MissingToken : ErrorReason("Missing token")
...
}
l
However, we are not able to cast the result to an 
Response.Error
 .
It's unclear what you're trying to cast… Do you know?
d
Yeah, let me give some more context
Consider this way of making a request from swift calling a suspend function in the common module:
Copy code
api.someSuspendMethod(params...) { result, error in

}
where
result
could be a
Result.Success
or
Result.Error
. Makes sense? I’m not an ios dev, but as I have understood
result
could have a value or
error
can have a value, hence the
Option
wrapper. (Tho the errors will be in the
result
variables in this case). It seems like the
result
we receive is of type
Result.Success
even though its a
Result.Error
which we can see when printing the result to the console. So we want to cast the
result
variable to
Success
if it is a success or an
Error
if it is an error.
someSuspendMethod
is defined as something like this:
Copy code
suspend fun someSuspendMethod(params): Result<ResponseType> { ... }
Is anything else unclear?
And we’re using 1.4.10 if that’s relevant
l
Result.Error
or
Response.Error
? Juding from that, it seems that your issue takes its root in not knowing what type is exposed/used exactly in the API.
d
Yeah, the classes isnt named exactly what I wrote in this post, so I might mixed up the terms a bit while writing this question, sorry ’bout that 😅 But yeah, we figured it out! We could not cast the response object to an Error due to an issue saying something like
kotlinNothing
was not
ResponseType
. So changing the
Response
class from the question to this:
Copy code
sealed class Response<out T : Any> {
    data class Success<out T : Any>(val data: T) : Response<T>()
    data class Error<out T : Any>(val reason: ErrorReason) : Response<T>()
}
did the trick. Thansks for your time 🙌
👍 1