You need to name your lambda parameter, or, if it's not to be used, name it as _.
Like so:
fun example1() {
var t1 = Testing()
t1.test(1, "String") { bool ->
// Do stuff
}
}
fun example2() {
var t1 = Testing()
t1.test(1, "String", { bool ->
// Do stuff
})
}
I presume you want to invoke your callback in the test method in Testing class. In which case you need to provide the argument to the function in order to invoke the lambda with the arg. can do it like this:
class Testing() {
var var1 = 0
var str1 = ""
var b = false
fun test(var1: Int, str1: String, lambdaArg: Boolean, lambda1: (Boolean)->Unit){
this.var1 = var1
this.str1 = str1
// Invoke the callback
lambda1(lambdaArg)
}
}
Or, if the arg to be passed to the lambda is a function of what happens in your test function, then you can ommit providing the lambda arg to test and instead hardcode your arg in the call to the lambda like this:
class Testing() {
var var1 = 0
var str1 = ""
var b = false
fun test(var1: Int, str1: String, lambda1: (Boolean)->Unit){
this.var1 = var1
this.str1 = str1
if (this.var1 == 0) {
lambda1(false)
} else {
lambda1(true)
}
}
}
test-function with yourt2-assignment. In thetest-implementation you may want to calllambda1(yourBoolean)(orlambda1.invoke(yourBoolean))... how you get that boolean is up to you... in the call oftestyou have the boolean available in the part where you wrote// do another thingusingit... you can also name that parameter if you wish, e.g.yourBool -> println("do something with your $yourBool")testis a function itself... you can't just pass the boolean value. The boolean value is supposed to be delivered from within thetest-function... at least that is, what I get from that signature ;-)testfrom outside, you have to use the callback function to decide what will happen with that given boolean value...