Safe navigation operator

Chad J chadjoan at __spam.is.bad__gmail.com
Sun Feb 19 18:35:24 PST 2012


On 02/19/2012 08:00 PM, Bernard Helyer wrote:
> On Monday, 20 February 2012 at 00:55:15 UTC, Alex Rønne Petersen wrote:
>> Hi,
>>
>> http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator
>>
>> What do you folks think about this? Would it be useful to have in D?
>> It would make navigating large object graphs with possible null values
>> much easier.
>
> The traditional counter point to this is that it encourages bad/lazy
> design. I don't really agree, but I don't ache for it either.

I wonder then, if this argument has been made:

I think non-nullable types would be /far/ more helpful for this.

As far as I can tell, the intend of having a "better" design would be to 
find undesired nulls as close to their origination point as possible.  I 
really think there have to be more methodical ways to do this (ex: 
non-nullable types) that don't rely on fallable human whim.  After all, 
if I need the ?. and don't have it, I'll just write an if-statement 
instead.  It'll be the same poor design, and it will be more difficult 
to read too.

Or maybe... in debug mode references could be fat pointers that have 
both the data ptr and a second integer that points to information about 
where the last assignment took place.

struct PhatRef(T)
{
	T* data;
	AssignmentEntry* lastAssignment;
}

struct AssignmentEntry
{
	string fileName;
	size_t lineNumber;
}


Then this:

void main()
{
	Foo foo;
}

Gets lowered by the compiler into this:

void main()
{
	Foo foo;
	foo.lastAssignment.fileName   = "main.d";
	foo.lastAssignment.lineNumber = 2;
}

When the null foo is referenced, the crash handler/debugger/whatever can 
traverse the AssignmentEntry structure and display the debugging info. 
This would save soooooo much time!


More information about the Digitalmars-d mailing list