Why does nobody seem to think that `null` is a serious problem in D?

Jordi Gutiérrez Hermoso jordigh at octave.org
Tue Nov 20 00:30:44 UTC 2018


On Monday, 19 November 2018 at 21:57:11 UTC, Neia Neutuladh wrote:

> Programmers coming from nearly any language other than C++ 
> would find it expected and intuitive that declaring a class 
> instance variable leaves it null.

What do you think about making the syntax slightly more explicit 
and warn or possibly error out if you don't do it that way? Either

   SomeClass c = null;

or

   SomeClass c = new SomeClass();

and nothing else.

> The compiler *could* give you a warning that you're using an 
> uninitialized variable in a way that will lead to a segfault, 
> but that sort of flow analysis gets hard fast.

Nulls/Nones are always a big gap in a language's type system. A 
common alternative is to have some Option/Maybe type like Rust or 
Haskell or D's Variant. How about making that required to plug 
the null gap?

> If you wanted the default constructor to be called implicitly,

Yeah, maybe this bit of C++ syntax isn't the best idea. What 
about other alternatives?


More information about the Digitalmars-d-learn mailing list