Why can Lambda be used for the java.lang.Thread class, but not for MyThread ?

 interface MyRunnable{ fun run() } class MyThread(runnable : MyRunnable){ } fun test(){ Thread({}) // All Alright MyThread({}) //Exception. Type mismatch <<-- Why ? } 

Link to check out this example: https://try.kotlinlang.org/#/UserProjects/tbs79qfkh50psp7r3qrdrinrmt/sfkpjq1bjvg4r6d5rmnu6mp4a8

  • It seems so conceived : "It is also necessary, since its interchange; - zRrr

1 answer 1

Because @FunctionalInterface work only for Java interfaces, they are not needed for Kotlin - Kotlin has built-in functional types, i.e. you can write immediately:

 class MyThread(runnable : () -> Unit) 

And do not create a separate interface for this.