Opt-in non-null class references?

SimonN eiderdaus at gmail.com
Fri Mar 2 19:47:23 UTC 2018


On Thursday, 1 March 2018 at 16:37:02 UTC, aliak wrote:
> I've put up a little experiment
> If you're interested: https://github.com/aliak00/optional

Nice! Optional is like std's Nullable with extra bells and 
whistles to make it as painless as you can offer it. I've read 
all of Optional's source, skimming the dispatching introspection 
magic, and it's a concise wrapper that is as transparent as it 
can get.

I can envision using this Optional type whenever I want nullable 
class reference, and use normal D class references as 
non-nullable references together with a codebase-wide rule that 
assigning null to normal D references is always a bug.

Even the generated compiler error messages are good: Optional!C x 
= some(new C()); int y = x; // Error: Cannot implicitly convert 
from Optional!C to C.

> a.dispatch.i; // not ideal, but is at least safe no op.

Hah, I've toyed with some examples, and this stuck out as verbose 
because everything else was really smooth. Optional!int 
dispatches wonderfully without extra syntax, whereas class C {}; 
Optional!C needs the extra dispatch before calling methods.

-- Simon


More information about the Digitalmars-d mailing list