digitalmars.D.learn - Variable lengh arrays of fixed length arrays
- Derek Parnell (22/22) Aug 03 2006 What am I not understanding about these beasties...?
- Derek Parnell (34/57) Aug 03 2006 Ok, I found the syntax that works...
- Oskar Linde (15/79) Aug 03 2006 Yes, I have to put workarounds for static arrays in almost every array
- Derek Parnell (12/28) Aug 04 2006 Except that I don't care what C does, I'm working with D now.
- Jarrett Billingsley (6/7) Aug 04 2006 And Oskar was saying that Walter _does_ very much so :P (I agree with y...
- Bruno Medeiros (12/29) Aug 08 2006 Nor as inout parameters, nor as AA keys...
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
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
Derek Parnell wrote:On Fri, 4 Aug 2006 11:06:02 +1000, Derek Parnell wrote:Yes, I have to put workarounds for static arrays in almost every array template function I make.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.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
On Fri, 04 Aug 2006 08:50:40 +0200, Oskar Linde wrote:Except that I don't care what C does, I'm working with D now.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.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
"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
Oskar Linde wrote: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#DIf 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.
Aug 08 2006