hey all, I keep running into this error on iOS whe...
# multiplatform
r
hey all, I keep running into this error on iOS when trying to cast a type of
Any
to a more concrete type
Copy code
Could not cast value of type 'kobjcc0'
I think this may be due to some issues with generics. I’m using an Either/Result class to wrap my return type in my Kotlin Native code. In iOS, it apparently doesn’t see the type at all, and instead refers to it as
kobjcc0
The frustrating part is that when I print the description of my object out in Xcode, the object that I want is there in the type I expect it to be represented (as a pair)
I’ve tried using the generics compiler flag
Copy code
binaries.framework {
     freeCompilerArgs += "-Xobjc-generics"
}
But it didn’t really help with this case
Here’s what my Either class looks like
Copy code
sealed class Either<out L, out R> {
    data class Error<out L>(val value: L) : Either<L, Nothing>()
    data class Success<out R>(val value: R) : Either<Nothing, R>()

    val isSuccess get() = this is Success<R>
    val isError get() = this is Error<L>

    fun <L> error(value: L) = Error(value)
    fun <R> success(value: R) = Success(value)

    fun either(fnL: (L) -> Any, fnR: (R) -> Any): Any =
            when (this) {
                is Error -> fnL(value)
                is Success -> fnR(value)
            }

    suspend fun eitherSuspend(fnL: suspend (L) -> Any, fnR: suspend (R) -> Any): Any =
            when (this) {
                is Error -> fnL(value)
                is Success -> fnR(value)
            }
}
a
Hello! Am I getting it correct, that everything breaks when you do something like
ReturnedSuccessWithAnyValue.value as! String
? I just tried with a simple piece of code but cannot reproduce the error for now.
r
Make sure your generic parameters have nonnull upper bounds (eg
L : Any
) or else objc will see them as
Any?
. Might not be the only issue you're having though.
r
Yeah, I made sure of that, thanks @russhwolf
@Artyom Degtyarev [JB] I'm not sure what you mean, since I usually have to access the Either values by using the either folding function
Here's the project repo if it helps https://github.com/RyanSimon/kmp-experiment
The problem specifically manifests at the ViewController call site in the iOS project
b
Def recommend filing a K/N GitHub issue
👍 1
r
okay, thanks @basher. i looked at the work @kpgalligan did on generics, and it seems like my repo respects the limitations. but 🤷‍♂️
this worked for me
Copy code
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink>().configureEach {
    this.binary.freeCompilerArgs += "-Xobjc-generics"
}
i was just messing up the compiler args
b
ah 👍
r
since i was using cocoapods plugin, i needed to override the binary args for ios
👍 2