-3

I have this Java class that I want to use from Kotlin code.

class JavaDialogBuilder<B extends JavaDialogBuilder>

This java code compiles fine:

void javaTest() {
    new JavaDialogBuilder();
}

Converting it to the Kotlin equivalent code does not.

fun javaTest() {
        JavaDialogBuilder() // <-- compilation error here
}

The compilation error is:

Type inference failed: Not enough information to infer parameter B in 

constructor JavaDialogBuilder<B : (JavaDialogBuilder<JavaDialogBuilder<*>>..JavaDialogBuilder<out JavaDialogBuilder<*>>?)>
( )

Please specify it explicitly.

Is there a way to fix this?

ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107
  • 1
    Actually, you are doing it wrong in Java and you should fix your Java code, not your Kotlin code. The Java compiler emits a "raw type" warning and your code is not type-safe if you get those. This is for backward compatibility with old code written before generics. Kotlin doesn't need to take that into account because the Kotlin language was created with generics and doesn't need to be compatible with old code. – Erwin Bolwidt Nov 15 '18 at 23:27
  • @ErwinBolwidt I completely agree! Unfortunately the Java code is not under my control, but I wanted to extend it with a Kotlin class – ZakTaccardi Nov 16 '18 at 14:49

1 Answers1

-1

You can create a Kotlin-specific subclass

class KotlinDialogBuilder : JavaDialogBuilder<KotlinDialogBuilder>()

This is not ideal - is there a way to do this without creating an extra subclass?

ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107