Empty VS null array?

Regan Heath regan at netmail.co.nz
Mon Oct 21 08:41:23 PDT 2013


On Mon, 21 Oct 2013 15:01:04 +0100, H. S. Teoh <hsteoh at quickfur.ath.cx>  
wrote:

> On Mon, Oct 21, 2013 at 11:53:44AM +0100, Regan Heath wrote:
>> On Fri, 18 Oct 2013 18:38:12 +0100, H. S. Teoh
>> <hsteoh at quickfur.ath.cx> wrote:
> [...]
>> >Conceptually speaking, an array is a sequence of values of
>> >non-negative length. An array with non-zero length contains at least
>> >one element, and is therefore non-empty, whereas an array with zero
>> >length is empty. Same thing goes with a slice. A slice is a view into
>> >zero or more array elements. A slice with zero length is empty, and a
>> >slice with non-zero length contains at least one element.
>>
>> This describes the empty/not empty distinction.
>>
>> >There's nowhere in this conceptual scheme for such a thing as a "null
>> >array" that's distinct from an empty array.
>>
>> And this is the problem/complaint.  You cannot represent specified/not
>> specified, you can only represent empty/not empty.
>>
>> I agree you cannot logically have an existing array that is somehow a
>> "null array" and distinct/different from an empty array, but that's
>> not what I want/am asking for.  I want to use an array 'reference' to
>> represent that the array is non existent, has not been set, has not
>> been defined, etc.  This is what null is for.
>
> The thing is, D slices are value types even though the elements they
> point to are pointed to by reference. If you treat slices (slices
> themselves, that is, not the elements they refer to) as value types,
> then the problem goes away. If you want to have a *reference* to a
> slice, then you simply write T[]* and then it becomes nullable as
> expected.

True, and that's a pointer, and I am comfortable using pointers.. however  
I worry this will limit the compilers ability to optimise somehow.. and  
doesn't it make the code immediately un"safe"?

> I do agree that the current situation is confusing, though, mainly
> because you can write `if (arr is null)`, which then makes you think of
> it as a reference type. I think that should be prohibited, and slices
> should be treated as pure value types, and all comparisons should be
> checked with .length (or .empty if you import std.range).

IMO, this would be preferable to the current situation even thought I  
would rather go the other way and have a reference type.  I can see the  
argument that it would be safer and easier for most users, even though I  
do not believe I am in that category.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list