Initialization of dynamic multidimensional array

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 10 01:29:17 PST 2017


On Friday, 10 February 2017 at 09:03:16 UTC, berni wrote:

>
> Now I tried this with a named instead of a magic constant e.g.
>
>> immutable VALUE=-1;
>> arr.each!"a[]=VALUE";
>
> And it doesn't work anymore. I've no clue, why... Can you help 
> me?

each is a template. As per the template documentation [1], your 
instantiation of each knows nothing about VALUE, because VALUE 
declared in the scope in which the template is instantiated. 
Template instantiations only have the scope in which they are 
implemented, not where they are instantiated, so here each cannot 
see VALUE.

On an side note (unrelated to your error), when declaring 
constants that are intended to be symbolic (i.e. you never need 
to take their address), it's more idiomatic to use manifest 
constants (via enum) rather than immutable.

enum value = -1;

[1] https://dlang.org/spec/template.html#instantiation_scope


More information about the Digitalmars-d-learn mailing list