For example:
fun f(func: () -> Any?)
Parameter func means a function which returns Any? type. But how to make func nullable like:
fun f( (func: () -> Any?)? )
Kotlin has a safe call operator (?.) to handle null references. This operator executes any action only when the reference has a non-null value. Otherwise, it returns a null value. The safe call operator combines a null check along with a method call in a single expression.
Kotlin gives us the power to declare high-order functions. In a high-order function, we can pass and return functions as parameters.
To define a function in Kotlin, fun keyword is used. Then comes the name of the function (identifier). Here, the name of the function is callMe . In the above program, the parenthesis ( ) is empty.
Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.
You are not too far away, I think you just misplaced the parentheses.
Try with:
fun f(func: (() -> Any?)?)
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