Thread overview
Using "[]" for empty array (instead of null)
Mar 01, 2006
Lionello Lunesu
Mar 01, 2006
Lionello Lunesu
Mar 01, 2006
Lionello Lunesu
Mar 01, 2006
Chris Sauls
Re: Using
Mar 01, 2006
pragma
March 01, 2006
Subject says it all, what do you think?

I always found the "char[] ar = null;" suspicious. An array is not (just) a pointer. What happens with its length?

# if (ar == null)

Is "ar" an array, or pointer?
Is only the pointer tested? or the length too? and/or?
Shouldn't it be "is null"?

Some examples using "[]" :

# if (ar == [])    // is 'ar' empty? (meaning length==0)

# char[] somefunc() {
#   return [];        // return an empty array (by definition ptr null,
length 0)
# }

I know it's not important and I can live well without it. Just musing. : )

L.


March 01, 2006
Lionello Lunesu wrote:

> Subject says it all, what do you think?
> 
> I always found the "char[] ar = null;" suspicious. An array is not (just) a pointer. What happens with its length?

Sounds OK, but doesn't it require (the long-lost) array literals first ?

void main()
{
  char ar[] = [];
}

variable test.main.ar is not a static and cannot have static initializer

--anders
March 01, 2006
> Sounds OK, but doesn't it require (the long-lost) array literals first ?

Yes, but this could be seen as a step towards those ; )

L.


March 01, 2006
Futhermore, "[]" does not suffer from the same problems. What's the type of "[2]"? Will "ar=[2]" copy the data, dup it or just point to it?

"Lionello Lunesu" <lio@remove.lunesu.com> wrote in message news:du40jt$7f8$1@digitaldaemon.com...
>> Sounds OK, but doesn't it require (the long-lost) array literals first ?
>
> Yes, but this could be seen as a step towards those ; )
>
> L.
> 


March 01, 2006
Lionello Lunesu wrote:
> Futhermore, "[]" does not suffer from the same problems. What's the type of "[2]"? Will "ar=[2]" copy the data, dup it or just point to it?
> 

BTW, is there a very good reason why D uses []'s instead of {}'s? This makes it hard to create intuitive/consistent array literals in the future.

For example, what should this do?

 int[] foobar;
 <some code here>
 foobar = [2];

a) replace foobar contents with the element '2'
b) create a new dynamic array that contains '2'
c) create a new dynamic array with a length of 2

In Java all arrays are objects so it's convenient to create a new array with 'new' but how should it be done in D? I guess the syntax should be left open so that it's possible to fill in the array literals in the future:

 Foobar a,b,c;
 <some code here>
 Foobar[] table = [ a, b, c ]; or
 Foobar[] table = { a, b, c }; or even
 Foobar[] table = new { a, b, c };

It should be someday also possible to create dynamic arrays of a given length without this much clutter:

 type[] array;
 array.length = x;

It should be like

 type[] array = type[x];

or something similar.

Multidimensional arrays are a bit difficult too:

 type[][] array;
 array.length = y;
 foreach(inout type[] row; array) row.length = x;

I would be very happy, if it could be just

 type[][] array = type[y,x]; or
 type[][] array = type[y][x]; or even
 array.length = y, x;


-- 
Jari-Matti
March 01, 2006
In article <du3pd9$30tq$1@digitaldaemon.com>, Lionello Lunesu says...
>
>Subject says it all, what do you think?
>
>I always found the "char[] ar = null;" suspicious. An array is not (just) a pointer. What happens with its length?
>
># if (ar == null)
>
>Is "ar" an array, or pointer?
>Is only the pointer tested? or the length too? and/or?
>Shouldn't it be "is null"?
>
>Some examples using "[]" :
>
># if (ar == [])    // is 'ar' empty? (meaning length==0)
>
># char[] somefunc() {
>#   return [];        // return an empty array (by definition ptr null,
>length 0)
># }
>
>I know it's not important and I can live well without it. Just musing. : )
>
>L.

Personally, I've always used .init whenever I had to guarantee a valid array, even if it was empty.

# int[] arr = int[].init;

It gets a little sticky with compound array types, as the compiler doesn't like this syntax:

# char[][char[]] dictionary = char[][char[]].init;

So I wind up having to use parentheses or an alias to tighten it up:

# char[][char[]] dictionary = (char[][char[]]).init;

# alias char[][char[]] Dictionary;
# Dictionary dictionary = Dictionary.init;

- EricAnderton at yahoo
March 01, 2006
Jari-Matti Mäkelä wrote:
> It should be someday also possible to create dynamic arrays of a given
> length without this much clutter:
> 
>  type[] array;
>  array.length = x;

Currently:
# type[] array = new type[x];

> 
> Multidimensional arrays are a bit difficult too:
> 
>  type[][] array;
>  array.length = y;
>  foreach(inout type[] row; array) row.length = x;

Currently:
# type[][] array = new type[y][];
# foreach (inout row; array) row = new type[x];

I would love to see the ability to do:
# type[][] array = new type[y][x];

As to the subject of array literals, I've always been fond of this syntax:
# int[] foo = new int[] [1, 2, 3];

However, it does suffer a couple of problems.  For one, because it uses []'s instead of {}'s or ()'s, it makes for a parsing trap.  In most cases, this is escapable because we already know the type we are expecting (from the decleration before the initializer) so we can just count bracket pairs, but what if we want to use 'auto'?

# auto foo = new int[] [1, 2, 3];

In this case, assuming D gets static matrices in the future (which would be nice), this would probably assign to foo an array of three dimensional matrices of volume 1x2x3.  Not quite what we wanted!!

I would say go back to using {}'s, and perhaps prepend struct initializers with a 'new' instruction as well.  Of course, if we get this 'local' instruction for stack allocating, then one could opt to use that instead.

# auto foo = new int[] {1, 2, 3};
# auto bar = local MyStruct {"tag"c, some_flags, foo};

-- Chris Nicholson-Sauls
March 01, 2006
Chris Sauls wrote:
> Jari-Matti Mäkelä wrote:
>> It should be someday also possible to create dynamic arrays of a given length without this much clutter:
>>
>>  type[] array;
>>  array.length = x;
> 
> Currently:
> # type[] array = new type[x];

Thanks, didn't know that yet. Haven't been much around lately. Time to read through the docs again :)

> 
> I would love to see the ability to do:
> # type[][] array = new type[y][x];
> 

I think some problems will appear when you want to combine different types of arrays here. E.g. part of the array structure is static, part of it dynamic.

> As to the subject of array literals, I've always been fond of this syntax: # int[] foo = new int[] [1, 2, 3];
> 
> However, it does suffer a couple of problems.  For one, because it uses []'s instead of {}'s or ()'s, it makes for a parsing trap.  In most cases, this is escapable because we already know the type we are expecting (from the decleration before the initializer) so we can just count bracket pairs, but what if we want to use 'auto'?
> 
> # auto foo = new int[] [1, 2, 3];
> 

What about dynamic arrays as function parameters? (I need them a lot)

 foo(new int[] [1,2,3]);

> I would say go back to using {}'s, and perhaps prepend struct initializers with a 'new' instruction as well.  Of course, if we get this 'local' instruction for stack allocating, then one could opt to use that instead.

I agree.

-- 
Jari-Matti