Keyword to avoid not null references

Namespace rswhite4 at googlemail.com
Sun Apr 22 00:51:59 PDT 2012


On Sunday, 22 April 2012 at 00:06:47 UTC, Adam D. Ruppe wrote:
> I just pushed an update to git with
> a few changes people have suggested.
>
> I removed one of the old opAssigns
> to keep a static type check.
>
> Added assumeNotNull and checkNotNull
> as entry points.
>
> Made the data member private.
>
>
> I think that's closer to what everyone
> wants.

Great work, but strange that the invariant break the whole code.

Has anyone here an idea, how a implicit cast can work?
Something like that should work:

void foo(Foo f) { }
void bar(NotNull!(Foo) f) { }

Foo f = new Foo();

foo(f);
bar(f);

At the moment you must do it with

bar(assumeNotNull(f));

and so you have twice as much work as actually being necessary. 
So in my opinion it must exist a way to avoid a explicit "cast" 
with assumeNotNull(f) on the caller side. Otherwise a check with 
assert(obj !is null); before you pass it to the method and then 
in the method again would do the same, and it's the same overhead.
I think without the possibility to "cast" Foo without any 
explicit function call to NotNull!(Foo), there are not much 
advantages.

What do you say?



More information about the Digitalmars-d-learn mailing list