I'm using Kotlin and I'm trying to pass a lambda as a function parameter
var lambda = {existingEntity: SomeEntity?, param1: String? -> {
val test = someFunction(existingEntity)
existingEntity?.copy(name = param1)!!
}}
fun updateValue(param2: UUID, param1: String? = null): Mono<Boolean> {
return updateFunc(param2, param1, lambda)
}
private fun updateFunc(param2: UUID, param1: String?, lambda: (SomeEntity?, String?) -> () -> SomeEntity): Mono<Boolean>{
return repository.findById(param2)
.flatMap {
val existingEntity = it
repository.update(existingEntity, lambda(it, param1))
.map { true }
}
}
Basically I want the lambda to return type SomeEntity
. With the above implementation, the lambda returns () -> SomeEntity
instead of SomeEntity
. This causes an issue while calling repository.update()
.
How do I write the lambda to return SomeEntity
instead of () -> SomeEntity
?
Whenever you wrap a fragment of code into {}
you are getting a lambda. For example:
val v = "I am a value!" // String
val v = {
"I am a function!"
} // () -> String
So...
val lambda: (SomeEntity?, String?) -> SomeEntity = { existingEntity: SomeEntity?, param1: String? ->
val test = someFunction(existingEntity)
existingEntity?.copy(name = param1)!!
}
As you can see what I did was removing the {}
braces inside a lambda.