Null is considered bad, because of nullcheck. My question is, what other way there is, that would have replaced this null problem? How could that have been avoided?
Asked
Active
Viewed 124 times
Maybe
monad orOption
type is that the type system requires you to check whether the value is null. Ifx
is an instance of a nullable type, Java allows you to dox.foo()
which can throw a NullPointerException. IfmaybeX
is an Option,maybeX.foo()
won't compile. In manyOption
implementations you could domaybeX.get().foo()
which is equivalent to not checking for a null pointer, but there are stricter variants where you have to have to supply code paths for both alternatives. E.g. in Scala:val y = maybeX getOrElse { throw SomeException() } + 2
– amon Oct 03 '15 at 12:33List<T>
. The backing store needs to be an array with more elements than the list has items, and there's really no sensible value that can be placed in the unused slots. One could have code generation generate run-time null checks for non-nullable generic types and omit such checks for nullable ones, but that would impose added costs at run-time without necessarily providing any useful safety improvement. – supercat Oct 29 '15 at 15:13T?
would be used internally, but the list itself could guarantee it only emitsT
and only allowsT
to be added unlessT?
was specified as the generic type (all garanteed at compile time). Obviously the entire language would have to support this and I think its probably too late to introduce it to java now. Putting aT?
in aT
list would be the same as putting aCat
in aT
list, garanteed at compile time – Richard Tingle Oct 29 '15 at 16:26