I was playing around with context receivers (added the opt-in compiler argument). The following code compiles, but throws a ClassFormatException
:
package test
interface Semigroup<A> {
fun op(x: A, y: A): A
}
object PlusSemigroup : Semigroup<Int> {
override fun op(x: Int, y: Int): Int = x + y
}
data class Box<A>(val value: A)
context(Semigroup<A>)
interface BoxSemigroup<A> : Semigroup<Box<A>> {
override fun op(x: Box<A>, y: Box<A>): Box<A> =
Box(op(x.value, y.value))
}
fun <A> boxSemigroup(underlying: Semigroup<A>): BoxSemigroup<A> =
object : BoxSemigroup<A> {}
fun main() {
val boxSemigroup = boxSemigroup(PlusSemigroup)
println(boxSemigroup.op(Box(2), Box(5)))
}
I get the following error:
Error: Unable to initialize main class test.TestKt
Caused by: java.lang.ClassFormatError: Illegal field modifiers in class test/BoxSemigroup: 0x1012
I tried different versions of this, e.g. also using with(underlying)
in the boxSemigroup
function, but it made no difference.
I’m using Java 17 (OpenJDK Runtime Environment (build 17+35-2724), Windows) and Kotlin 1.8.0
Before I file a bug, I wanted to ensure that I don’t do something stupid, and that others can confirm this behavior.