3 ms·
> With Optional you have two problems, you can get a null AND you can get an empty. That's not really the fault of the Optional. That's Java adding null as val
by grumpyprole 4y ago
> With Optional you have two problems, you can get a null AND you can get an empty.
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.
- PaulHoule 4y agoI'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.
- ryanianian 4y agoStatic 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.