(non)nullable types

Michel Fortin michel.fortin at michelf.com
Fri Feb 13 13:45:12 PST 2009


On 2009-02-13 14:01:57 -0500, "Nick Sabalausky" <a at a.a> said:

>> Or use an existing syntax:
>> 
>> Foo? foo = ...;
>> if (Foo value = foo) {
>> // value is a local variable that is only accessible if foo is not
>> null
>> } else {
>> // value is not accessible here
>> }
> 
> I could live with that, but I'd prefer my suggestion because it wouldn't
> require the creation of an extra label for what's essentially the same
> variable. With your code we'd just end up with a whole bunch of:
> 
> Foo? myObj = ...;
> if (Foo nonnullMyObj = myObj) //etc...
> 
> // or
> 
> Foo? nullableMyObj = ...;
> if (Foo myObj = nullableMyObj) //etc...
> 
> ...Which just seems unnecessary to me.

The problem with retyping the same variable is that it may gives 
strange situations. For instance, is this an error?

	Foo? myObj = ...;
	if (myObj !is null)
	{
		doSomethingWith(myObj);
		myObj = null; // should this be an error?
	}

And what about:

	Foo? myObj = ...;
	while (myObj !is null)
		myObj = myObj.next;

If you want to avoid verbosity, you'll need to retype the variable 
until it gets assigned another potentially non-null value. Basically, 
you'll need to perform flow analysis inside the function to enforce 
that.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list