www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array initialization

reply bobef <bobef lessequal.com> writes:
I've been using D for quite time now and I don't even remember what I 
miss from C++ anymore. Except for this little thing that keeps annoying 
me every time...

char[][] a=["asdasd","12313123","vclkvlckjvlkc"];
asd.d(5): variable asd.main.a is not a static and cannot have static 
initializer

Can someone enlighten me why 'a' should be static? This seems nonsense 
to me... Maybe it has something to do with the garbage collector, or?
Feb 10 2006
parent reply "John C" <johnch_atms hotmail.com> writes:
"bobef" <bobef lessequal.com> wrote in message 
news:dsi4g3$vfp$1 digitaldaemon.com...
 I've been using D for quite time now and I don't even remember what I miss 
 from C++ anymore. Except for this little thing that keeps annoying me 
 every time...

 char[][] a=["asdasd","12313123","vclkvlckjvlkc"];
 asd.d(5): variable asd.main.a is not a static and cannot have static 
 initializer

 Can someone enlighten me why 'a' should be static? This seems nonsense to 
 me... Maybe it has something to do with the garbage collector, or?
Dynamic array initialization isn't implemented in the compiler yet. Apparently it will be introduced after D reaches 1.0. In the meantime there exist various techniques you can use to initialize the array, such as: template arrayOf(T) { T[] arrayOf(T[] params ...) { return params.dup; } } char[][] a = arrayOf!(char[])("One", "Two", "Three");
Feb 10 2006
next sibling parent "Derek Parnell" <derek psych.ward> writes:
On Sat, 11 Feb 2006 01:16:45 +1100, John C <johnch_atms hotmail.com> wrote:

 "bobef" <bobef lessequal.com> wrote in message
 news:dsi4g3$vfp$1 digitaldaemon.com...
 I've been using D for quite time now and I don't even remember what I  
 miss
 from C++ anymore. Except for this little thing that keeps annoying me
 every time...

 char[][] a=["asdasd","12313123","vclkvlckjvlkc"];
 asd.d(5): variable asd.main.a is not a static and cannot have static
 initializer

 Can someone enlighten me why 'a' should be static? This seems nonsense  
 to
 me... Maybe it has something to do with the garbage collector, or?
Dynamic array initialization isn't implemented in the compiler yet. Apparently it will be introduced after D reaches 1.0.
Should that be "Compile-time array initialization, except for strings isn't implemented in the compiler yet."
 In the meantime there exist various techniques you can use to initialize  
 the
 array, such as:

     template arrayOf(T) {
         T[] arrayOf(T[] params ...) {
             return params.dup;
         }
     }

     char[][] a = arrayOf!(char[])("One", "Two", "Three");
And of course, this template only works inside of functions; it does work at the module level. To have module level arrays 'initialized' you need to use the module constructor. // ---- example ----- import std.stdio; char[][] a; static this() { a = arrayOf!(char[])("One", "Two", "Three"); } void main() { foreach(char[] s; a) writefln(s); } -- Derek Parnell Melbourne, Australia
Feb 10 2006
prev sibling parent reply Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
John C wrote:
 "bobef" <bobef lessequal.com> wrote in message 
 news:dsi4g3$vfp$1 digitaldaemon.com...
 I've been using D for quite time now and I don't even remember what I miss 
 from C++ anymore. Except for this little thing that keeps annoying me 
 every time...

 char[][] a=["asdasd","12313123","vclkvlckjvlkc"];
 asd.d(5): variable asd.main.a is not a static and cannot have static 
 initializer

 Can someone enlighten me why 'a' should be static? This seems nonsense to 
 me... Maybe it has something to do with the garbage collector, or?
Dynamic array initialization isn't implemented in the compiler yet. Apparently it will be introduced after D reaches 1.0. In the meantime there exist various techniques you can use to initialize the array, such as: template arrayOf(T) { T[] arrayOf(T[] params ...) { return params.dup; } } char[][] a = arrayOf!(char[])("One", "Two", "Three");
After 1.0 ? Maybe that's array literals, because compile-time array initialization should be very easy to implement. Here's another alternative technique, which also illustrates what the compiler should do: static char[][3] _ar_static_initer = ["asdasd","2313123","vclkvlck"]; char[][] a = _ar_static_initer; -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Feb 10 2006
parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Bruno Medeiros wrote:
 John C wrote:
 
 "bobef" <bobef lessequal.com> wrote in message 
 news:dsi4g3$vfp$1 digitaldaemon.com...

 I've been using D for quite time now and I don't even remember what I 
 miss from C++ anymore. Except for this little thing that keeps 
 annoying me every time...

 char[][] a=["asdasd","12313123","vclkvlckjvlkc"];
 asd.d(5): variable asd.main.a is not a static and cannot have static 
 initializer

 Can someone enlighten me why 'a' should be static? This seems 
 nonsense to me... Maybe it has something to do with the garbage 
 collector, or?
Dynamic array initialization isn't implemented in the compiler yet. Apparently it will be introduced after D reaches 1.0. In the meantime there exist various techniques you can use to initialize the array, such as: template arrayOf(T) { T[] arrayOf(T[] params ...) { return params.dup; } } char[][] a = arrayOf!(char[])("One", "Two", "Three");
After 1.0 ? Maybe that's array literals, because compile-time array initialization should be very easy to implement. Here's another alternative technique, which also illustrates what the compiler should do: static char[][3] _ar_static_initer = ["asdasd","2313123","vclkvlck"]; char[][] a = _ar_static_initer;
It shouldn't do exactly this because _ar_static_initer is an array literal and cannot contain variables or runtime-computed parts.
Feb 10 2006