0

I'm having trouble enforcing type safety when using kotlin with Java. Please see the code snippets below showing setup, the lack of compile error is the issue.

// kotlin functions

inline fun <reified T:Any> k() = T::class

// A<T> is a Java class

fun <T:Any> a(clazz:KClass<T>): A<T> {
    return A<T>(clazz.java)
}


// java interface

interface DoA<T> {
   void doThis(A<T> a);
}

// kotlin class implements java interface

class SomeDoA : DoA<String> {
    init {
        doThis(a(k<Int>())); // I'd expect a compile error here, but none
    }
}
newlogic
  • 807
  • 8
  • 25
  • 1
    How is your class `A` declared? When I declare it as `open class A(kClass: KClass)`, I get the compile error you expect. (Also, your code won't compile because of `do` being a keyword, I had to rename it) – hotkey May 11 '18 at 09:55
  • A is a Java class, sure I've renamed it in the example. – newlogic May 12 '18 at 11:36

0 Answers0