null and type safety
Steven Schveighoffer
schveiguy at yahoo.com
Wed Nov 5 10:08:01 PST 2008
"Walter Bright" wrote
> Jarrett Billingsley wrote:
>> Don't you think that eliminating something that's
>> always a bug at compile time is a worthwhile investment?
>
> Not always. There's a commensurate increase in complexity that may not
> make it worth while.
>
> My focus is on eliminating bugs that cannot be reliably detected even at
> run time. This will be a big win for D.
I was working in C# today, and I realized one very excellent design
advantage for D -- the array. In C#, if null, an array is subject to null
dereference errors. In D, it simply doesn't happen, because the array has a
guard that is stored with the reference -- the length. I think these
similar to the kinds of things that Jarrett is referring to. Something
that's like a pointer, but can't ever be null, so you never have to check it
for null before using it. Except Jarrett's idea eliminates it at compile
time vs. run time.
Couldn't one design a struct wrapper that implements this behavior?
Something like:
NonNullable!(T)
{
opAssign(T t) {/* make sure t is not null */}
opAssign(NonNullable!(T) t) {/* no null check */}
...
}
-Steve
More information about the Digitalmars-d
mailing list