Incorrect conversion for inner class anonymous extension

Given the following class structure:

public class A {
    public class B {
        void b() {}
    }
}

and extending B anonymously like so:

A a = new A();
A.B b = a.new B() {
    void b() {
        System.out.println();
    }
}

converting above code to Kotlin yields a compilation error:

In addition, I can’t find the correct syntax for above construct.

IntelliJ version: 2021.1.3 (Ultimate edition) Build #IU-211.7628.21
Kotlin plugin version: 211-1.5.10-release-909-IJ7142.45

You can do this using with():

val a = A()
val b = with(a) {
    object : A.B() {
        fun a() {
            println()
        }
    }
}

But maybe there is a better way.

P.S. For god’s sake, don’t post your code examples as images!

2 Likes

Thanks @broot . Updated my post. However with your example, I’m getting a Kotlin internal compilation error: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Failed to generate expression: KtObjectLiteralExpression
Caused by: java.lang.AssertionError: Non-outer parameter incorrectly mapped to outer for public constructor <no name provided>()

Hmm, it works for me without problems.

Is it a Gradle project? If so what is the version of Kotlin in build.gradle(.kts) file? I think by default Intellij doesn’t use a compiler provided by the IDE plugin, but by Gradle.