Hello, I am trying to use <context receivers> in t...
# multiplatform
d
Hello, I am trying to use context receivers in the internal multiplatform code. I have something like this:
Copy code
internal interface Logging {
    val log: Logger
}

context(Logging) @HiddenFromObjC
internal fun <T> Iterable<T>.firstOrLogNull(): T? {
    val first = this.firstOrNull()
    if (first != null) {
        return first
    } else {
        log.warn { "Iterable is empty!" }
        return null
    }
}
All works fine on Android, but iOS app just crashes with
A bad access to memory
when accessing
firstOfNull()
. Any ideas on how to get around that?
e
context receivers were only experimentally implemented on JVM and they're going away in the next Kotlin version anyway (to be replaced with context parameters later)
d
Well, my thinking was that the code is internal/private, so the iOS should not be bothered about it
@ephemient May I have one more question? Do you know if the context parameters (once they come out) will be implemented on iOS like this, or am I going to have to wait for them to get implemented later?
c
Context parameters will first come as a prototype. The team has not said whether the prototype will be multiplatform or not, but since K2, I think it's possible that it will. There will be an unknown length of time for testing and assessment, before stabilization. Probably at least a year. If the prototype is successful, then it will be stabilized into the language, and at that point context parameters will be guaranteed to be multiplatform, yes.
e
the previous prototype also existed in K2, and was still only implemented in the JVM backend. I don't have any information about whether that will be the case or not for the successor
d
Hmm, ok, thank very much. Well, I don't mind it being experimental. It's not a critical system or anything and I feel like the experimental features like this work fine like 99% of time anyway. From looking at the discussion regarding the context parameters it seems like it is merged and therefore should be released in 2.0.30? I'm gonna ask about the multiplaform there.
👀 1