[:] as empty associative array literal, plus warning for null

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Jul 5 08:47:32 PDT 2013


05-Jul-2013 16:26, Artur Skawina пишет:
> On 07/05/13 12:39, Dmitry Olshansky wrote:
>> 05-Jul-2013 13:24, Regan Heath пишет:
>>> 1. Arrays are a thin wrapper around a reference type (ptr) which add
>>> safety.
>>
>> Rather it packs 2 pointers (pair: ptr, ptr+len), modeling the region in between.
>> No matter how we look at it, it doesn't overlap with most of pointer operations except for indexing. In my opinion it can't be framed as a thin wrapper around _one_ pointer.
>
> The 'array' term is overloaded and confusing, let's call them "slices".
>
> Slices are nothing but fat pointers;
> they just carry more information -
> - the length.
>

The interesting fact is that slice introduces a new concept - empty 
slice. It bears no relation to a view of "wrapped" pointer at all.

(0-pointer is not "empty" it can't be empty it's just invalid)

>>> 2. When you have a thin wrapper you should treat operations on the
>>> wrapper as the wrapped object in the general case.
>>
>> Continuation of the above stretch.
>> To be a true wrapper it has to support only the same or subset of operations. For instance arrays have slicing operation hence it's more then that.
>
>     ubyte* pointer = null;
>     auto slice = pointer[0..42];
>

I'd argue that this creates a slice, but point taken. Slicing aside 
there this empty notion. My point was the space between 2 pointers != 
pointer semantically hence doesn't need to behave like one no matter the 
statement.

It's like saying that an integer is the same as an interval of integers 
because it only adds an extra length field to a starting point. Concept 
is different.

>
>>> 3. if (x) should compare x to 0.
>>
>> This one is consistent.
>
> Actually, slices should implicitly convert to bool with !!length.
> "Normal" pointers couldn't do that - because they had no length.
>

Aside of meta-arguments for me it ends like this:
1) disallow if(arr)
2) or make it the same as if(arr.length) as more useful/frequent/meaningful

The first step is required anyway if we are to change things.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list