Reputation: 4703
In the below code, what is wrong with the second add?
val lambdas = mutableListOf<()->Unit>()
lambdas.add{println("a")} // this compiles fine
lambdas.add{()->println("b")} //why can't I do this?
error: expecting a name
lambdas.add{{()->println("b")}}
Upvotes: 2
Views: 156
Reputation: 3890
You can't declare a lambda the way you are trying to
val right: () -> Int = { 1 } // Convenient way to declare a lambda without parameters
val alsoRight: () -> Int = { -> 1 } // The right way to explicitly declare a lambda without parameters
val wrong: () -> Int = { () -> 1 } // The wrong way to declare a lambda without parameters
That line should look like this:
lambdas.add { -> println("b") }
Upvotes: 3