What I've tried so far is callback with single parameter and it works:
class SomeClass (something:Int = 3, val callback: (Int) -> Unit) {
fun doSomething() {
callback(11)
}
}
class AnotherClass {
val something = SomeClass({onSomething(it)})
protected fun onSomething(num: Int) {
// ...
}
}
But how to implemented it with multiple parameters like:
class SomeClass (something:Int = 3, val callback: (Int, String) -> Unit) {
fun doSomething() {
callback(11, "Yeah")
}
}
class AnotherClass {
val something = SomeClass(/* ...... what goes here???? */)
protected fun onSomething(num: Int, str: String) {
// ...
}
}
Just use the lambda expression syntax with explicit parameters:
val something = SomeClass { num, str -> onSomething(num, str) }
When passing a lambda as the last parameter, you can omit the parenthesis.
Also, you can use a bound function reference when the expected and actual function signatures match exactly:
val something = SomeClass(this::onSomething)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With