Non-nullable references, again

Benji Smith dlanguage at benjismith.net
Wed Dec 31 14:24:09 PST 2008


Don wrote:
> Denis Koroskin wrote:
>> Foo nonNull = new Foo();
>> Foo? possiblyNull = null;
 >
> Wouldn't this cause ambiguity with the "?:" operator?

At first, thought you might be right, and that there would some 
ambiguity calling constructors of nullable classes (especially given 
optional parentheses).

But for the life of me, I couldn't come up with a truly ambiguous 
example, that couldn't be resolved with an extra token or two of lookahead.

The '?' nullable-type operator is only used  in type declarations, not 
in expressions, and the '?:' operator always consumes a few trailing 
expressions.

Also (at least in C#) the null-coalesce operator (which converts 
nullable objects to either a non-null instance or a default value) looks 
like this:

   MyClass? myNullableObj = getNullableFromSomewhere();
   MyClass myNonNullObj = myNullableObj ?? DEFAULT_VALUE;

Since the double-hook is a single token, it's also unambiguous to parse.

--benji



More information about the Digitalmars-d mailing list