www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is NullableRef checked at compile time?

reply cy <dlang verge.info.tm> writes:
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.

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.
May 23 2016
next sibling parent Levi Maclean <LeviMaclean inbound.plus> writes:
On Monday, 23 May 2016 at 18:10:14 UTC, 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 checked at compile time only if you "hold the door".^^
May 23 2016
prev sibling parent ag0aep6g <anonymous example.com> writes:
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.
May 23 2016