I think I don't understand what is the diff between Unit and {}, e.g. when using callback in a fun.
fun x(
callback: () -> Unit = {} // fine
)
fun x(
callback: () -> Unit = Unit // not fine
)
{} is a lambda that returns Unit, which is a valid value for () -> Unit.
Unit is an object, which is not a valid value for () -> Unit.
Unit is an object with no methods or properties. It's just the default return value for any function. If your function doesn't specify a return type, then that's implicitly returning Unit.
{} is a lambda function that takes no parameters and - since it's the default - returns Unit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With