I believe the only current way to do this is to change your definition of buz and set it as a property:
val buz: (String)->Unit = { m->
println("another message: $m")
}
However, there has been some discussions about this here, and I think supporting passing named functions like that makes sense. I'd also like to be able to do this:
class Zoo {
fun buz(m: String) { }
}
val z = Zoo()
foo(“a message”, z.buz)
This would allow the callback to have the context of an object. You can currently do this by wrapping it:
We are currently working on it. The syntax will be the following:
``
foo(“a message”, myPackage::buz) // myPackage is a name of the package
foo(“a message”, ::buz) // buz will be looked up in current scope, works if “buz” is declared in current package or imported
In the nearest future, we'll support only syntax with class or package before "::" symbols. Mentioned use case will be covered by standard library function taking object and member function, returning function with "captured" receiver object, used like this:
bind(myobject, buz)
Later, we are planning to support syntax you offer (with expression before “::”), but exact syntax may appear a bit different.
This syntax will let us just pass the member function as a parameter to other function or will return it as a function?. I mean, this will be possible?:
This will be a syntactic sugar for trivial function literal which passes all its parameters to referred function. Answering your questions, yes it will return function expression, and your sample will work.