www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Using "[]" for empty array (instead of null)

reply "Lionello Lunesu" <lio remove.lunesu.com> writes:
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?



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 "[]" :





length 0)


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

L. 
Mar 01 2006
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Mar 01 2006
parent reply "Lionello Lunesu" <lio remove.lunesu.com> writes:
 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.
Mar 01 2006
parent reply "Lionello Lunesu" <lio remove.lunesu.com> writes:
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.
Mar 01 2006
parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
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
Mar 01 2006
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
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:
 
 Multidimensional arrays are a bit difficult too:
 
  type[][] array;
  array.length = y;
  foreach(inout type[] row; array) row.length = x;
Currently: I would love to see the ability to do: As to the subject of array literals, I've always been fond of this syntax: 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'? 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. -- Chris Nicholson-Sauls
Mar 01 2006
parent =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
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:
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:

 
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:

 
 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'?
 

 
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
Mar 01 2006
prev sibling parent pragma <pragma_member pathlink.com> writes:
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?



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 "[]" :





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. It gets a little sticky with compound array types, as the compiler doesn't like this syntax: So I wind up having to use parentheses or an alias to tighten it up: - EricAnderton at yahoo
Mar 01 2006