Thread overview
Example error for initializer?
Jun 21, 2006
Paolo Invernizzi
Jun 21, 2006
Tom S
Jun 22, 2006
Bruno Medeiros
Jun 21, 2006
Chris Miller
Jun 21, 2006
Chris Miller
June 21, 2006
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
June 21, 2006
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 +/
June 21, 2006
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;
June 21, 2006
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).

# T[] array (T) (T[] vals ...) { return vals; }
#
# int[] def = array(1, 2, 3);

-- Chris Nicholson-Sauls
June 21, 2006
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 = [ 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).
>
> # T[] array (T) (T[] vals ...) { return vals; }
> #
> # int[] def = array(1, 2, 3);
>
> -- Chris Nicholson-Sauls

Make that
   T[] array (T) (T[] vals ...) { return vals.dup; }
or it refers to old stack memory.
June 22, 2006
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