Is there a cleaner way to pass in an empty trailin...
# getting-started
j
Is there a cleaner way to pass in an empty trailing lambda besides this?
Copy code
val example = funWithLambdaParam(arg1) {} // This just looks ugly to me
I was hoping I could just pass in something like
::Unit
but that doesn't seem to be a feature
l
I think
funWithLambda(arg1, {})
should still work just as well, no?
j
That's arguably worse because now I have a squiggly line under it since intellij wants me to move the lambda outside the parentheses
l
Silly IntelliJ…
lol 1
r
if
funWithLambda
is your own function, you could simply add
{}
as the default value of your parameter, like
fun funWithLambda(arg1: Int, f: () -> Unit = {}) { ... }
otherwise you could define a constant somewhere like
val noOp = {}
and pass that to
funWithLambda
instead, but it would only work for lambda's without parameters of course
j
To my brain, I feel like
::Unit
would be a nice way to represent "a function which takes no parameters and returns Unit" but it's just syntax sugar blob shrug If this wasn't kotlin with all of its bells and whistles, to be fair, I wouldn't be complaining
w
If that's your opinion, nothing stops you from defining the function:
Copy code
fun Unit(): Unit = Unit
l
I feel a Yo Dawg meme is appropriate here.
xzibit 1
k
When I type the lambda
{}
IntelliJ warns me that
it
is an unused parameter and suggests to change it to
{ _ -> }
🤮
j
I guess this will do
Copy code
val example = funWithLambda(arg1, fun() = Unit)
It is objectively more
fun
😂 3
🦆 1
🤔 1