www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Example error for initializer?

reply Paolo Invernizzi <arathorn NOSPAM_fastwebnet.it> writes:
Hi all,

In http://www.digitalmars.com/d/arrays.html under Pointer Arithmetic 
there's:

int[] def = [ 1, 2, 3 ];	// dynamic array of 3 ints

but does not compile (dmd .0161):

   variable xdelta.main.def is not a static and cannot have static 
initializer

Example error?

---
Paolo Invernizzi
Jun 21 2006
next sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Paolo Invernizzi wrote:
 Hi all,
 
 In http://www.digitalmars.com/d/arrays.html under Pointer Arithmetic 
 there's:
 
 int[] def = [ 1, 2, 3 ];    // dynamic array of 3 ints
 
 but does not compile (dmd .0161):
 
   variable xdelta.main.def is not a static and cannot have static 
 initializer
Currently only static arrays can have static initializers, so if you declare the 'def' inside some function, in your case 'main', it must be declared like this: static int[] def = [1, 2, 3]; Declarations at global scope are static anyway, so the error does not occur in that case - and that's the case in the example. -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jun 21 2006
parent Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
Tom S wrote:
 Paolo Invernizzi wrote:
 Hi all,

 In http://www.digitalmars.com/d/arrays.html under Pointer Arithmetic 
 there's:

 int[] def = [ 1, 2, 3 ];    // dynamic array of 3 ints

 but does not compile (dmd .0161):

   variable xdelta.main.def is not a static and cannot have static 
 initializer
Currently only static arrays can have static initializers, so if you
And by static arrays he means static-storage arrays, and not static-length arrays. (The term static is ambiguous for arrays. The alternatives for those two are respectively, local-storage, dynamic-length) -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Jun 22 2006
prev sibling parent reply "Chris Miller" <chris dprogramming.com> writes:
On Wed, 21 Jun 2006 17:01:11 -0400, Paolo Invernizzi  =

<arathorn NOSPAM_fastwebnet.it> wrote:

 Hi all,

 In http://www.digitalmars.com/d/arrays.html under Pointer Arithmetic  =
 there's:

 int[] def =3D [ 1, 2, 3 ];	// dynamic array of 3 ints

 but does not compile (dmd .0161):

    variable xdelta.main.def is not a static and cannot have static  =
 initializer

 Example error?

 ---
 Paolo Invernizzi
DMD doesn't support it yet for non-static, non-const; here is a workarou= nd: const int[] _def_init =3D [ 1, 2, 3 ]; int[] def =3D _def_init;
Jun 21 2006
parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Chris Miller wrote:
 On Wed, 21 Jun 2006 17:01:11 -0400, Paolo Invernizzi  
 <arathorn NOSPAM_fastwebnet.it> wrote:
 
 Hi all,

 In http://www.digitalmars.com/d/arrays.html under Pointer Arithmetic  
 there's:

 int[] def = [ 1, 2, 3 ];    // dynamic array of 3 ints

 but does not compile (dmd .0161):

    variable xdelta.main.def is not a static and cannot have static  
 initializer

 Example error?

 ---
 Paolo Invernizzi
DMD doesn't support it yet for non-static, non-const; here is a workaround: const int[] _def_init = [ 1, 2, 3 ]; int[] def = _def_init;
Or use a templated variadic function. Should be factored out by the optimizer and/or inliner (I would imagine). -- Chris Nicholson-Sauls
Jun 21 2006
parent "Chris Miller" <chris dprogramming.com> writes:
On Wed, 21 Jun 2006 11:53:26 -0400, Chris Nicholson-Sauls  =

<ibisbasenji gmail.com> wrote:

 Chris Miller wrote:
 On Wed, 21 Jun 2006 17:01:11 -0400, Paolo Invernizzi   =
 <arathorn NOSPAM_fastwebnet.it> wrote:

 Hi all,

 In http://www.digitalmars.com/d/arrays.html under Pointer Arithmetic=
=
 there's:

 int[] def =3D [ 1, 2, 3 ];    // dynamic array of 3 ints

 but does not compile (dmd .0161):

    variable xdelta.main.def is not a static and cannot have static  =
=
 initializer

 Example error?

 ---
 Paolo Invernizzi
DMD doesn't support it yet for non-static, non-const; here is a =
 workaround:
     const int[] _def_init =3D [ 1, 2, 3 ];
    int[] def =3D _def_init;
Or use a templated variadic function. Should be factored out by the =
 optimizer and/or inliner (I would imagine).





 -- Chris Nicholson-Sauls
Make that T[] array (T) (T[] vals ...) { return vals.dup; } or it refers to old stack memory.
Jun 21 2006