Signed word lengths and indexes

bearophile bearophileHUGS at lycos.com
Thu Jun 17 14:16:56 PDT 2010


Andrei Alexandrescu:
> Just like with non-null references, Walter has 
> framed the matter in a way that makes convincing extremely difficult. 
> That would be great if he were right.

I know this is off-topic in this thread. I remember the long thread about this. Making all D references nonnull on default requires a significant change in both the language and the way objects are used in D, so I can understand that Walter has refused this idea, maybe he is right.

But something more moderate can be done, keep the references nullable on default, and it can be invented a symbol (like @) that can be added as suffix to a class reference type or pointer type, that denotes it is nonnull (and the type system can enforce it at the calling point too, etc, it's part of the function signature or variable type, so it's more than just syntax sugar for a null test inside the function!).

I believe this reduced idea can be enough to avoid many null-derived bugs, it's different from the situation of the Java exceptions, it's less viral, if you write a 100 lines long D program, or a long C-style D program, you are probably free to never use this feature.

void foo(int*@ ptr, Bar@ b) {...}

void main() {
    int*@ p = ensureNonull(cast(int*)malloc(int.sizeof));
    Bar@ b = ensureNonull(new Bar());
    foo(p, b);
}

Something (badly named) like ensureNonull() changes the input type into a notnull type and performs a run-time test of not-null-ty :-)

Surely this idea has some holes, but they can probably be fixed. 

Bye,
bearophile


More information about the Digitalmars-d mailing list