Issue with default parameter in operator overload

I have a small repro case here:

class OperatorBug {
    fun setValue(k: String, v: Int) = println("$k: $v")
}

operator fun OperatorBug.set(k: String, arg: Char = 'd', v: Int) {
    when (arg) {
        'd' -> setValue(k, v)
        else -> println("Got non-default value for 'arg': $arg")
    }
}

fun main(args:Array<String>) {
    val ob = OperatorBug()
    ob["foo"] = 6
}

When I run this program, I expect it to print “foo”: 6

But ‘arg’ doesn’t get its default value of ‘d’:

~>kotlinc OperatorBug.kt
~>kotlin OperatorBugKt
Got non-default value for ‘arg’: ^@

It’s actually getting ‘\u0000’, a hard 0, which seems to imply that it’s uninitialized.

Is this the correct behavior? It seems unintuitive.

1 Like

I also can reproduce it. It’s evidently a bug to me :wink:

I created an issue for it here: https://youtrack.jetbrains.com/issue/KT-38599?project=kt

Thanks!