Tuang
09/25/2019, 8:58 AMhandler: (key: K) -> V?) means
In the following function
fun getOrPut(key: String, handler: (key: String) -> V?)
i thinks handler is something likes the arguments name, so what are the rest?
is it a lambda ?diesieben07
09/25/2019, 9:01 AMhandler is a parameter with a function type. You could pass a lambda here, for example.
https://kotlinlang.org/docs/reference/lambdas.htmlDrasko Saric
09/25/2019, 9:31 AMgetOrPut(...) receives variable named key of type String, which will be used as argument (or variable which will be used for calculating the argument ) of function handler(...) when invoking.Tuang
09/25/2019, 9:46 AMgetOrPut() function.
may be like this ?
getOrPut("first arg", sampleFun("first arg"))Drasko Saric
09/25/2019, 9:51 AMAlowaniak
09/25/2019, 9:52 AMsampleFun would return a function itself then yes.
But most probably getOrPut("first arg", ::sampleFun) or getOrPut("first arg") { sampleFun(it) }Drasko Saric
09/25/2019, 9:56 AMsampleFun return function itself, but I think it is maybe confusing to @Tuang.Tuang
09/25/2019, 9:57 AMDrasko Saric
09/25/2019, 10:05 AMDrasko Saric
09/25/2019, 10:06 AMgetOrPut function and function used for calling getOrPut.Tuang
09/25/2019, 10:12 AMTuang
09/25/2019, 10:29 AMDrasko Saric
09/25/2019, 10:31 AMgetOrPut("first arg", sampleFun("first arg")) would make sense only when sampleFun(..) would return a function. 🙂Tuang
09/25/2019, 10:32 AMMatteo Mirk
09/25/2019, 12:43 PMhandler is just a type declaration (like String), only that it’s a function type, so it accepts any function reference. The function type specifies its input and output types. So like others showed above, that parameter can accept a function/method reference or a lambda.Alowaniak
09/25/2019, 1:03 PMval myPrint: (x: Any) -> Unit = ::println myPrint("foo")