Would it make sense to add `typealias Null = Nothi...
# stdlib
r
Would it make sense to add
typealias Null = Nothing?
to the stdlib (and preamble)? Common use case:
Copy code
class Info<T>(val description: String, val payload: T, ...)

interface Base<T> {
    fun getInfo(): List<Info<T>>
}

class Derived: Base<Null> {
    override fun getInfo() = listOf(Info("...", null, ...), ...)
}
IMO it reads easier than
class Derived: Base<Nothing?>
. (Yes, I understand I could use
Unit
in this case to achieve basically the same thing in this contrived example. That's beside the point.)
i
An interesting idea. Do you think there are many cases where
Nothing?
is used and where
Null
would be more expressive?
It's interesting to consider what is primary and what is derivative. E.g.
Null
can be a typealias to
Nothing?
, and
Nothing?
can be considered as a union of
Nothing | Null
Though at this point, introducing
Null
identifier in a package imported by default would be rather disturbing. There can be objects named
Null
(https://github.com/pact-foundation/pact-jvm/blob/7cd4ff4d3be7f8e4a3534ae67c3ef18d0b998dc8/core/support/src/main/kotlin/au/com/dius/pact/core/support/json/JsonValue.kt#L41) and they can lead to a conflict or confusion when being in scope with the
Null
typealias.
r
That's a good point. I imagine
Null
is probably a common class name for a number of serialization / data interchange libraries. As far as how many cases there are (where
Nothing?
is used and where
Null
would be more expressive), I'm not sure how to gauge that. While I use it in a number of my projects, the use case is admittedly pretty niche. I'd guess that most cases where
Nothing?
is used,
Null
would be more expressive (at least I can't think of a good counter example right now).
When it comes to primary vs derivative, that's an interesting chicken / egg scenario. I guess it would make more sense to forgo
Null
altogether, and have
null
be both a value and a type, though then it feels like we're stepping on `Unit`'s toes a bit.