0

From my understanding, if a Kotlin method requires one argument and that argument is an interface with a single method, I can pass in a lambda expression instead (as illustrated by numerous online tutorials using setOnClickListener and stuff like that). However, compiling the following code:

interface I {
    fun ha()
}

class C() {
    fun set(i: I) { i.ha() }
}

fun main(args: Array<String>) {
    val c = C()
    c.set { println("Hi") }
}

results in: error: type mismatch: inferred type is () -> Unit but I was expected.

Did I missing something?

Nick Lee
  • 5,639
  • 3
  • 27
  • 35
  • Possible duplicate of [Redundant SAM-constructor can't be remove for Kotlin declared function, but works on Java declared function](https://stackoverflow.com/questions/46051572/redundant-sam-constructor-cant-be-remove-for-kotlin-declared-function-but-work) and [Why do Lambda expressions behave differently for Kotlin and Java classes?](https://stackoverflow.com/questions/44324821/why-do-lambda-expressions-behave-differently-for-kotlin-and-java-classes) – Salem Mar 06 '18 at 06:10
  • Thanks guys, if only I search for the proper phrases. – Nick Lee Mar 06 '18 at 06:15

0 Answers0