Null references redux

Justin no at spam.com
Fri Oct 2 05:08:06 PDT 2009


bearophile Wrote:

> Max Samukha:
> 
> > Don't get confused by 'new' in struct initializers. Structs in C# are
> > value types.
> 
> Yes, you are right.
> 
> But in D structs can be allocated on the heap too, so I think having optional nonnull
> struct pointers can be useful. The syntax and usage is similar to normal struct pointers.
> 
> Bye,
> bearophile

AYK, in C++ structs are just classes with public protection (for members) by default, or, if you like,
classes are just structs with private protection (for members) by default.  Other than protection rules
there aint any *useful* difference semantically and you can have pointers or references to both.

To a certain extent Bjarne would have done C++ a favour by either (1)
not introducing "class" as a separate language entity and simply resigning to

#define class struct

and adding the OO the features to struct,
or (2), instead of bleeding OO features into structs, heeding

"teacher, teacher leave the struct alone"

and introducing "class" as the clean addition that C++ brought to C.

With D, the designer has made some rather brilliant distinction between classes and structs
in terms of value vs reference.  In doing so he has sorted out the dot (.) vs arrow (->) mess
that is the infamous signature of C++.

Quoting from the D spec:

"Structs and unions are meant as simple aggregations of data, or as a way to paint a
data structure over hardware or an external type. External types can be defined by the operating
system API, or by a file format. Object oriented features are provided with the class data type."

I think it would be a billion dollar mistake to make the last sentence in that quote obsolete.

But, if people insist, then let's be consistent and apply the same logic (re optional
nonnull pointers being useful) to unions as well (and, for that matter, to anything that
can be pointed to).  Ahggg, shock horror.

But then again, out of the shock and horror, maybe something beautiful can evolve.

Cheers

-- Justin Johansson




More information about the Digitalmars-d mailing list