Keyword to avoid not null references
Timon Gehr
timon.gehr at gmx.ch
Sat Apr 21 15:55:44 PDT 2012
On 04/22/2012 12:48 AM, Namespace wrote:
> On Saturday, 21 April 2012 at 22:18:02 UTC, Adam D. Ruppe wrote:
>> We can do not null in the library reasonably
>> well. I have a basic one in github:
>>
>> https://github.com/D-Programming-Language/phobos/pull/477
>
> So every time i want to avoid null references i have to write
> "NotNull!(Foo) f" (or better, because it's a struct: "ref NotNull!(Foo)
> f")? And therefore i must initialize them with NotNull!(Foo) f = new
> Foo();? That would be a little annoying. What if i needed in function
> bar only Foo f which can be null but in quatz i need a not null Reference?
> In my opinion the best way would be to initialize them with Foo f = new
> Foo(); and if i pass them to bar, it will be implicit cast to
> NotNull!(Foo).
But this implies a runtime check.
> I think, that would be the best idea.
> The only thing that disturbing me, is, that it is a struct and therefore
> it passes by value instead as reference
You might have a wrong mental model of how classes and structs work in D.
A class reference is much like a struct of the following form:
struct ClassRef{
ClassImpl* impl;
}
> and that it is more to write as
> just "@" or "@ref".
And only the trivial cases are catched during compilation.
More information about the Digitalmars-d-learn
mailing list