I would like to overload an operator like invoke for null literal.
For example, so I could writen something like
null(1, 2) Is it possible?
I wouldn't advise overloading anything on the null literal, as it would cause too much confusion and would have no real-world use case.
However, technically, it's possible:
operator fun Nothing?.invoke() = "hi!" fun main() { println(null()) // Works, prints "hi!" val x = null println(x()) // Also works. val y: String? = null //println(y()) // compilation error val z: String = "" //println(z()) // also compilation error } It makes use of the fact that the null keyword has the nullable type Nothing?.
nullkeyword? pl.kotl.in/PIcDxr-5pnullkeyword exclusively". If there's some function intended only to be called onnull(which still makes no sense to me), then you shouldn't be able to call it on other objects.