https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

cdpjenkins

10/01/2021, 10:52 AM
Is there a function in the Kotlin standard library for that ignores its argument and doesn’t do anything? There have been a few places where I’ve needed to provide a handler to satisfy an API but where I don’t want anything to happen when the handler has been called. I have found myself writing
fun <T> doNothing(param: T): Unit = Unit
or an equivalent lambda and I’m wondering if there is a standard function that I can do in order to not have to write this function myself.
j

Joffrey

10/01/2021, 11:05 AM
If you need to pass a function, you can just pass
{ Unit }
. If you need to implement a method (like an override), you can just use
Unit
as your implementation body
c

cdpjenkins

10/01/2021, 11:29 AM
Many thanks. I didn’t realise that it’s possible to do something a simple as`{ Unit }` so that might be enough for me 😺
e

ephemient

10/01/2021, 5:56 PM
even
{ }
should work, although explicitly stating
Unit
is clearer
🙏 1
7 Views