References in D

Jonathan M Davis jmdavisProg at gmx.com
Sat Sep 15 11:58:11 PDT 2012


On Saturday, September 15, 2012 20:24:52 Henning Pohl wrote:
> On Saturday, 15 September 2012 at 18:05:55 UTC, Jonathan M Davis
> 
> wrote:
> > I'd argue that using null for indicating something other than
> > the lack of a
> > value is bad design. But there are plenty of cases where being
> > able to
> > indicate that there is no value is useful.
> 
> I agree.
> 
> > And if a function requires that a
> > pointer or reference or array or whatever have a value, then
> > there's always
> > DbC or exceptions.
> 
> So why not clear this up at compile time if possible? Then you
> can also easily distinguish between "really there" and "maybe
> there" objects by passing either a reference or a pointer.
> 
> > Just because someone can misuse a feature  doesn't mean that
> > a feature shouldn't be there.
> 
> Why not set the "non-nullable references", which cannot be
> misused, as default and still enable the "nullable references"
> feature as optional by passing a pointer.

You can't have pointers to class objects in D. You can have pointers to 
references but not to the objects that they point to, because the type system 
does not have the concept of a class object separate from a reference. And 
such pointers are non-polymorphic to boot. So, it doesn't make any sense at 
all to try and pass around pointers to classes as a solution for much of 
anythnig.

Regardless, at this point, D has nullable references, and that's not going to 
change. It's far too late for that. Non-nullable references could be added at 
some point in the future, but at this point, Walter and Andrei both think that 
having a NonNullable struct in the standard library which wraps a reference 
and asserts that it's non-null is good enough and that changing the language 
isn't worth it, especially since we're trying to fully stabilize the language 
and not be adding features unless we really need them and they can't 
reasonably be done in the library. And while it may not be quite as good as 
having non-nullable references in the language, you _can_ create a library 
solution for them, and such a solution is going to be added to the standard 
library.

It may be that if/when D3 materializes that non-nullable references will be 
added to the language, but that's years off at this point.

- Jonathan M Davis


More information about the Digitalmars-d mailing list