Adding the ?. null verification

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 18 17:34:46 PDT 2014


On Wed, Jun 18, 2014 at 12:36:01PM -0700, H. S. Teoh via Digitalmars-d wrote:
> On Wed, Jun 18, 2014 at 07:04:33PM +0000, via Digitalmars-d wrote:
[...]
> > The expression needs to have exactly one type, and because all of the
> > components can be non-null, it needs to be the type of the last
> > component, in this case `val`. This means that if the one of the
> > components is null, the entire expression needs to return a value of
> > this type, presumably the `.init` value.
> > 
> > The alternative is to raise an exception (or error), but that would
> > defeat the purpose (almost, as it would be slightly better than
> > segfaulting).
> 
> Here's a first stab at a library solution:
[...]

I decided to run some tests on the compiled code to see how performant
it was. With dmd, I was unable to get it to inline all the opDispatch
calls, even with -O -inline, so the result was rather disappointing.
However, with gdc -O3 -finline, all of the opDispatch calls got inlined,
and the assembly is the direct equivalent of:

	if (tree !is null)
		if (tree.left !is null)
			...
				writeln(tree.left.right. ... .val);

So this is evidence that even this preliminary implementation already
has rather good potential. Perhaps I'll kick up a pull request for it
soon. :)


T

-- 
In theory, software is implemented according to the design that has been
carefully worked out beforehand. In practice, design documents are
written after the fact to describe the sorry mess that has gone on
before.


More information about the Digitalmars-d mailing list