Worse is better. Ease of deployment over ease of development.
As for the Q: why design languages with null (that is - incomplete or flawed type systems) now? I have o idea. I think the reasoning is that “worse is better” succeeded for JS, php, C, so it’s a viable path.
“Maybe an X” and “Definitely an X” are more different than string and number. If a language pretends string and number are distinct but at the same time has no distiction for “maybe X” - then it doesn’t have a very good type system.
Note that it doesn’t necessarily need to avoid null values for this. Non-nullables is mostly equivalent although less elegant. That is, “String s” means a string or null, while “String! s” means a non-null string (example from a C# vNext syntax).
Different explanation of history: Something "wins" because it is precisely what is needed.
"My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement."
References in C++ are non-nullable so the precedent already existed.
I hope C# gets around to solving this problem -- Microsoft is working on it -- but it's much harder to retroactively solve it.
Wrong analogy. References in C++ are not first-class entities. Conceptually, they're _alias names_ for existing objects. Unlike C# and Java "references", you cannot "reset" a C++ reference to "point" to another object. Because it's not a pointer. It does not "point". It's an alias. `void f(int& ref)` in C++ is the same as `void f(ref int x)` in C# (the equivalent doesn't exist in java). Inside `f`, you cannot change `x` to "point to some other int" because it's not a pointer!
C# and Java "references" are semantically the same as C++ pointers minus arithmetic.
This could've been done since 2003 but wasn't. There are attempts to bring it into language as part of C++ Core Guidelines project.
This is possible because unlike Java and C# not everything can be null.
But you can entirely write a shitton of C++ programs that won't use pointers at all - not even shared / unique. In contrast, you can't avoid C# and Java references.
template<typename T> class List<T> {
protected:
List();
template<typename U> class Visitor<U> {
public:
U visitNil();
U visitCons(T current, U accumulator);
}
public:
virtual <U> U visit(Vistor<U> visitor);
}
template<typename T> class Nil<T> extends List<T> {
U visit(Visitor<U> visitor) = visitor.visitNil();
}
template<typename T> class Cons<T> extends List<T> {
private:
T head;
List<T> &tail;
public:
Cons(T head, List<T> &tail) {
this.head = head;
this.tail = tail;
}
U visit(Visitor<U> visitor) = visitor.visitCons(head, tail.visit(visitor));
}Then you've thrown in the towel when it comes to optimality. Not saying this is necessarily bad (maybe you're inventing the next Python), but acknowledge this isn't really an acceptable option for the next C.
type X struct{}
func (x *X) y() {}
func main() {
var x *X = nil
x.y() // no error
}
Also its not unsafe. Go doesn't allow unsafe memory access unless you use the unsafe package.It is incorrect though. An Go does allow you to write bugs.
res, err := http.Get("wcpgw")
defer res.Body.Close()
...
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x5ed9af]
But anyway, that's history, now we have nice languages that have safe nulls, so the interesting questions moving forward are: why are people still creating new languages that have unsafe nulls (looking at you Go), and why are people still choosing to use languages with unsafe nulls?