digitalmars.D - Array initialization
- bobef (8/8) Feb 10 2006 I've been using D for quite time now and I don't even remember what I
- John C (12/20) Feb 10 2006 Dynamic array initialization isn't implemented in the compiler yet.
- Derek Parnell (21/46) Feb 10 2006 Should that be "Compile-time array initialization, except for strings
- Bruno Medeiros (10/38) Feb 10 2006 After 1.0 ? Maybe that's array literals, because compile-time array
- Ivan Senji (3/42) Feb 10 2006 It shouldn't do exactly this because _ar_static_initer is an array
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
"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
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...Should that be "Compile-time array initialization, except for strings isn't implemented in the compiler yet."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");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
John C wrote:"bobef" <bobef lessequal.com> wrote in message news:dsi4g3$vfp$1 digitaldaemon.com...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."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
Bruno Medeiros wrote:John C wrote:It shouldn't do exactly this because _ar_static_initer is an array literal and cannot contain variables or runtime-computed parts."bobef" <bobef lessequal.com> wrote in message news:dsi4g3$vfp$1 digitaldaemon.com...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;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