3 ms·
This could be fixed in Java by two simple changes. x = func(null); // func is ignored and x is null proc(null); // proc is ignored and an NPE is thrown. and
by JulianMorrison 16y ago
This could be fixed in Java by two simple changes.
x = func(null); // func is ignored and x is null
proc(null); // proc is ignored and an NPE is thrown.
and so implicitly
y = a(b(c(d(null)))); // a b c d are ignored and y == null
This would mean you could treat null like Nothing, threading it through non side effecting functions and only testing the result. If you tried to use it with a side effect, it would halt.
The compiler would be free to omit these checks where x is provably not null. For example:
foo (x);
y = bar(x);
The assignment to y does not need a null check, because a null would have thrown NPE in foo.
Side effect of this design: code will never receive a null in parameters. Parameter values can be assumed to be definitely not-null by coder and compiler alike.
EDIT: one more change would be needed: prim = func(ref) is illegal unless ref can be proved not null. Also, add a no-op built-in, notnull(x) that does nothing except NPE if x is null. That could be used as proof to the compiler.
- masklinn 16y agoThis is an utterly terrible idea. Acceptable — barely — in dynamically typed languages, but utterly moronic for a strongly typed language where you can make references null-safe statically and at the type level. Hiding nulls under the carpet is only a recipe for painfully hard to discover bugs in the long run.
- JulianMorrison 16y agoThis design is type level. Properties and primitives are not null, references used after a guard procedure are not null, other references are Maybe types. Functions are Maybe typed if they are operating on nullable references. Those types are not syntactically top level, but that's because I was considering an alteration, not a rewrite.