www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variable lengh arrays of fixed length arrays

reply Derek Parnell <derek nomail.afraid.org> writes:
What am I not understanding about these beasties...?

This fails to compile ...

 void main()
 {
  char[9][] sstore;  // A dynamic array of 9-char strings.
  char[9] astr;      // A single 9-char string.

  // Extend the dynamic array.
  sstore.length = sstore.length + 1; 

  // Copy the 9-char string to the last entry.
  sstore[$-1] = astr;  // **FAILS** 
 }

The compiler message is ...

test.d(10): cannot assign to static array (sstore)[cast(int)((__dollar) -
1u)]

In my mind, 'sstore' is *not* a static array. It is a dynamic array that
contains static arrays.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
4/08/2006 11:03:26 AM
Aug 03 2006
parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Fri, 4 Aug 2006 11:06:02 +1000, Derek Parnell wrote:

 What am I not understanding about these beasties...?
 
 This fails to compile ...
 
  void main()
  {
   char[9][] sstore;  // A dynamic array of 9-char strings.
   char[9] astr;      // A single 9-char string.
 
   // Extend the dynamic array.
   sstore.length = sstore.length + 1; 
 
   // Copy the 9-char string to the last entry.
   sstore[$-1] = astr;  // **FAILS** 
  }
 
 The compiler message is ...
 
 test.d(10): cannot assign to static array (sstore)[cast(int)((__dollar) -
 1u)]
 
 In my mind, 'sstore' is *not* a static array. It is a dynamic array that
 contains static arrays.
Ok, I found the syntax that works... import std.stdio; void main() { char[9][] sstore; // A dynamic array of 9-char strings. char[9] astr; // A single 9-char string. // The [] is required! astr[] = "abcdefghi"; // Extend the dynamic array. sstore.length = sstore.length + 1; // Copy the 9-char string to the last entry. sstore[$-1][] = astr; writefln("%s %s", sstore.length, sstore[0]); } This is a stupid, stupid, stupid, wart on D. Why does the syntax for fixed-length arrays have to be different to everything else? Makes template programming harder than it should be. If one codes char[9] X; X = "abcdefghi"; Why on Earth would the compiler think I'm trying to do anything else but copy the data from the literal to the array? I'm mean...what else can I do? I can't change the 'reference' because there is no reference with fixed length arrays - they just exist as RAM - they are not a pseudo struct like variable length arrays. Walter, please justify this anomaly and why I should embrace this apparent lose of reason. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 4/08/2006 11:09:52 AM
Aug 03 2006
parent reply Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
Derek Parnell wrote:
 On Fri, 4 Aug 2006 11:06:02 +1000, Derek Parnell wrote:
 
 What am I not understanding about these beasties...?

 This fails to compile ...

  void main()
  {
   char[9][] sstore;  // A dynamic array of 9-char strings.
   char[9] astr;      // A single 9-char string.

   // Extend the dynamic array.
   sstore.length = sstore.length + 1; 

   // Copy the 9-char string to the last entry.
   sstore[$-1] = astr;  // **FAILS** 
  }

 The compiler message is ...

 test.d(10): cannot assign to static array (sstore)[cast(int)((__dollar) -
 1u)]

 In my mind, 'sstore' is *not* a static array. It is a dynamic array that
 contains static arrays.
Ok, I found the syntax that works... import std.stdio; void main() { char[9][] sstore; // A dynamic array of 9-char strings. char[9] astr; // A single 9-char string. // The [] is required! astr[] = "abcdefghi"; // Extend the dynamic array. sstore.length = sstore.length + 1; // Copy the 9-char string to the last entry. sstore[$-1][] = astr; writefln("%s %s", sstore.length, sstore[0]); } This is a stupid, stupid, stupid, wart on D. Why does the syntax for fixed-length arrays have to be different to everything else? Makes template programming harder than it should be.
Yes, I have to put workarounds for static arrays in almost every array template function I make.
 
 If one codes 
 
   char[9] X;
 
   X = "abcdefghi";
 
 Why on Earth would the compiler think I'm trying to do anything else but
 copy the data from the literal to the array? I'm mean...what else can I do?
 I can't change the 'reference' because there is no reference with fixed
 length arrays - they just exist as RAM - they are not a pseudo struct like
 variable length arrays. 
Not being able to assign static arrays also means that you can't use them as a return type from functions.
 Walter, please justify this anomaly and why I should embrace this apparent
 lose of reason.
I guess there is a very obvious answer. This is all how static arrays work in C. Making static arrays assignable (by copying the elements) means that static arrays would turn into value types, which IMHO is much better than their current state of being neither value nor reference types. It would affect functions like: void func(ubyte[10_000_000] buffer) {...} But I doubt such cases are very common in D code. Calling extern(C) functions would still require the passing of a reference of course. /Oskar
Aug 03 2006
next sibling parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Fri, 04 Aug 2006 08:50:40 +0200, Oskar Linde wrote:
 Walter, please justify this anomaly and why I should embrace this apparent
 lose of reason.
I guess there is a very obvious answer. This is all how static arrays work in C.
Except that I don't care what C does, I'm working with D now.
 Making static arrays assignable (by copying the elements) means that 
 static arrays would turn into value types, which IMHO is much better 
 than their current state of being neither value nor reference types.
 
 It would affect functions like:
 
 void func(ubyte[10_000_000] buffer) {...}
 
 But I doubt such cases are very common in D code. 
And the coder gets what they deserve in those cases <G>
 Calling extern(C) 
 functions would still require the passing of a reference of course.
That's why we have .ptr property for arrays. cFunc( fixedarray.ptr ); -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 4/08/2006 5:01:17 PM
Aug 04 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Derek Parnell" <derek nomail.afraid.org> wrote in message 
news:1mpsx243qleut.jg5857jswkq8.dlg 40tude.net...

 Except that I don't care what C does, I'm working with D now.
And Oskar was saying that Walter _does_ very much so :P (I agree with you, though) I totally agree that statically-sized arrays should be value types, and not "second-class" value types.
Aug 04 2006
prev sibling parent Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
Oskar Linde wrote:
 If one codes
   char[9] X;

   X = "abcdefghi";

 Why on Earth would the compiler think I'm trying to do anything else but
 copy the data from the literal to the array? I'm mean...what else can 
 I do?
 I can't change the 'reference' because there is no reference with fixed
 length arrays - they just exist as RAM - they are not a pseudo struct 
 like
 variable length arrays. 
Not being able to assign static arrays also means that you can't use them as a return type from functions.
Nor as inout parameters, nor as AA keys... "I have a dream... that one day the chains of discrimination shall be cast away from all specdom. That one day static arrays shall stand together with any value type, and look upon each other as equals! And that our children will live in a type system where all segregation and C-lavery have long been forgotten in the mists of alpha stages." - Martin Luther String ..or something like that ;) -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Aug 08 2006