Is NullableRef checked at compile time?
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 23 12:54:38 PDT 2016
On 05/23/2016 08:10 PM, cy wrote:
> I was squinting at the std.typecons.NullableRef code and it _looks_ like
> isNull is only checked at runtime (and not checked at all in release
> mode!) but D has surprised me before in its ability to pre-calculate
> stuff during compilation.
NullableRef is little more than a plain pointer. In particular, it
allows nulling the reference. Guarding against null dereferences doesn't
seem to be on its agenda at all.
> I was thinking of using something like this:
>
> http://arsdnet.net/dcode/notnullsimplified.d
>
> ...which does the check once (during runtime) but after that it's
> compile-time verified not to have a null pointer. (Sort of like C++
> references!) I think using NullableRef instead would have every instance
> of getting the pointer perform the check at runtime, even if it has
> already been verified as not null. That's correct, right? Yes I know,
> premature optimization etc, but I was just curious if a NotNull template
> might be a stronger declaration of a pointer's nullness.
Notice the different name. The goals of NotNull and NullableRef seem to
be entirely different.
Also note that NotNull uses `assert` for the check, too. So there's no
check with -release either. Can easily change that to `enforce`, of course.
More information about the Digitalmars-d-learn
mailing list