Opt-in non-null class references?

aliak something at something.com
Thu Mar 1 16:37:02 UTC 2018


On Wednesday, 28 February 2018 at 15:25:40 UTC, SimonN wrote:
> My gripe is that the necessarily-nullable class reference 
> doesn't express the intent.
> Either a codebase must rely on silent conventions or every 
> function with asserts.

I've put up a little experiment that you may be interested in for 
this part of the problem at least, also a little bit of compile 
time help because D treats dot on pointers and non pointers the 
same, it makes for some nice syntax:

class C {
     int i = 3;
}

auto a = no!C;

if (auto c = a.unwrap) {
     writeln("not here: ", c.i);
}
a.dispatch.i; // not ideal, but is at least safe no op.

a = some(new C());

if (auto c = a.unwrap) {
     writeln("here: ", c.i);
}

If you're interested: https://github.com/aliak00/optional

Syntax allowing:
Optional!C = null;

is not there yet, but I think is doable because typeof(null) is 
distinguishable. So with an optional type this can be well 
defined as "none" internally as well

Not ideal I know, but maybe a start to see where it can go?

> It's merely sad to see D, with all its powerful static 
> inspection, rely on runtime tests for nulls while other 
> languages (Kotlin, Zig, and 2017 C#) rule null out at 
> compile-time, as if it's the most natural thing in the world.

Yeah, I whole heartedly agree with this. Maybe is an exposure 
thing? I remember seeing a talk (by I think the kickstarter 
people) that said after migrating to swift (from objective-c) 
their crash reports because dropped significantly (my brain for 
some reason remembers this to be zero, but I'm not sure and I 
can't figure out which talk this was).

Cheers,
- Ali



More information about the Digitalmars-d mailing list