Hello. Is there something more elegant than the fo...
# getting-started
m
Hello. Is there something more elegant than the following to invoke each lambda in a list?
Copy code
lambdas.forEach { it() }
blob shrug 2
m
Isn't that pretty straight forward and elegant already?
4
m
yes it is pretty good. i was thinking someting like forEach(::invoke) or forEach(::it) if you get my intention.
m
Something like this, but I’m guessing I’m missing something from the
Function
type.
lambdas.forEach(Function::invoke
j
if it can be a reference, you can set it automatically from the IDE using the actions pop up.
m
That’s what I normally do.
m
it must be noted that the lambdas are
() -> Unit
.
if they were
(element) -> Unit
they could fall into the forEach signature
j
I don’t know if Element::invoke could work, I am not at the computer right now to do some tests
m
Element would only be the lambda.
m
No one stops you from creating an extension
lambdas.invokeAll()
🙂
☝️ 1
👍 1
Anyway, It will work as you wanted if you declare a type alias:
Copy code
typealias Work = () -> Unit

lambdas.forEach(Work::invoke)