References in D

Namespace rswhite4 at googlemail.com
Mon Sep 17 01:06:43 PDT 2012


On Monday, 17 September 2012 at 00:22:52 UTC, Jonathan M Davis 
wrote:
> On Monday, September 17, 2012 00:43:50 deadalnix wrote:
>> It shouldn't be that hard to create a Nullable!T template.
>
> We have one, and it would be wasteful to use that for 
> references or pointers
> when they're naturally nullable (though you're more or less 
> forced to if you
> want a truly nullable array thanks to the nonsense that empty 
> arrays and null
> arrays are considered equal). You're forced to have a separate 
> boolean value
> indicating whether it's null or not. That might make sense for 
> an int, since
> it can't be null, but pointers and references _can_ be and are 
> in every type
> system that I've ever used.
>
> Regardless, the solution at this point is going to be to add
> std.typecons.NonNullable. It would be in there already, but the 
> pull request
> with it needed more work, and it hasn't been resubmitted yet.
>
> - Jonathan M Davis

Instead of "NonNullable" a built-in operator would be preferable. 
Because it seems as though many would like to have something.

Short example:

void foo(Foo& f) { }
void main() {
     Foo& f1; // <-- error, not-null references declared, but not 
assigned.
     Foo f2; // <-- ok

     foo(f1); // <-- we can trust, f1 has a valid value
     foo(f2); /* we cannot trust, the compiler checks at runtime, 
if f2 has a valid value.*/


More information about the Digitalmars-d mailing list