A few questions
Marco Leise
Marco.Leise at gmx.de
Sat Jul 28 09:53:55 PDT 2012
Am Sat, 28 Jul 2012 05:07:44 +0200
schrieb "bearophile" <bearophileHUGS at lycos.com>:
> >> // x is a nullable class reference
> >> if (x == null) {
> >> ...
> >> } else {
> >> // here the type system sees x as not null.
> >> }
> >
> In the else branch the state of the type of x is not-null, so as
> example in the else branch you are allowed to call a function
> that only accept not null references, with the x variable.
> Not-nulls integrated in the type system makes sure you have well
> initialized variables in the class constructors in presence of
> inheritance and other complexities.
> It also statically requires you to test for null before
> deferencing a nullable class reference, ans so on.
> Those are the fruits that a good not-null implementation gives
> you. You can't do all this with the NotNull toy. NotNull solves
> only the easy part of the whole problem, and it's a small part.
>
> Bye,
> bearophile
I found this an interesting read. The implementation likely doesn't interfere much with other language features (in a sense like @trusted does to templated functions that take potentially unsafe code as parameters).
I especially like how the compiler _statically_ knows, that x is not null in the else case. The runtime cost is moved from every call on x, to a single if-statement! It could require a little logic to parse complex conditions like "if (a == 1 && !x != null)", though ;)
--
Marco
More information about the Digitalmars-d-learn
mailing list