Re:Java’s Cultural Problem
by ryanianian on Monday September 19, 2022 @10:34AM
(#32898889)
Static analysis can catch a `null` being used in a place that wants an Optional. With Optional being fully pervasive in a codebase the `null` concept could effectively be eliminated.
I'd grant that. I'd say the authentic-to-Java approach to the problem is to static import something like a "map" function
var result = map(x,x=>x.y)
which is equivalent to var result = isNull(x) ? null : x.y
where isNull is static imported from Objects.An approach which is authentic to some other languages is to consistently used a list with 0 or 1 members instead of optionals.
That's not really the fault of the Optional. That's Java adding null as value to every type. Java also allows lists of nulls, mappings to null etc. An optional is just a container that stores one item at most. I do agree that it makes optionals less useful in Java. In Haskell and OCaml they are used essentially instead of null.