Thread overview
Array initialization
Feb 10, 2006
bobef
Feb 10, 2006
John C
Feb 10, 2006
Derek Parnell
Feb 10, 2006
Bruno Medeiros
Feb 10, 2006
Ivan Senji
February 10, 2006
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?
February 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");


February 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...
>> 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
February 10, 2006
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."
February 10, 2006
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.