https://kotlinlang.org logo
#compiler
Title
# compiler
c

Christian Maus

06/28/2022, 5:49 PM
I’m not sure if I hit a compiler limitation or a bug regarding context receivers. In the following example, the value
program
, which is a contextual function can not be evaluated when the context parameter is brought into scope. Should I file a bug report? 🤔
Copy code
fun interface Context1 {
    fun c1(): Int
}

fun interface Context2 {
    fun c2(v: Int): String
}


context(Context1)
fun useC1(): context(Context2) (Int) -> String {
    val x = c1()
    return { c2(it + x) }
}

fun main(args: Array<String>) {
    val program: context(Context2) (Int) -> String = with(Context1 { 42 }) {
        useC1()
    }

    //this works
    val result = program(Context2 { "the value is $it" }, 2)
   
    //but this doesn't work
    with(Context2 { "the value is $it" }) {
        program(2)
    }
}
d

dmitriy.novozhilov

06/28/2022, 6:00 PM
Looks like a bug for me Please report an issue
c

Christian Maus

06/28/2022, 6:05 PM
Will do. Thank you
👍 1
y

Youssef Shoaib [MOD]

06/28/2022, 7:57 PM
@dmitriy.novozhilov Isn't this just the current lambda calling limitation? as in, to invoke a contextual lambda currently you need to provide it with all its arguments as parameters? AFAIK this is a deliberate limitation?
d

dmitriy.novozhilov

06/28/2022, 9:09 PM
Hm, indeed I forgot about that limitation
3 Views