How to declare static compile-time assoc array inside struct?

Timoses timosesu at gmail.com
Mon Aug 13 14:46:14 UTC 2018


On Monday, 13 August 2018 at 13:21:45 UTC, Andrey wrote:
> On Monday, 13 August 2018 at 11:53:06 UTC, rikki cattermole 
> wrote:
>> You must use a module constructor to initialize it.
>
> Tried this:
> static this()
> {
>     Test.DESCRIPTION = [Test.Type.One: "One!", Test.Type.Two: 
> "It's Two...", Test.Type.Three: "And... Three!"];
> }
>
> struct Test
> {
>     // ...
> }
>
> I have an error: cannot modify immutable expression DESCRIPTION

I suppose something along these lines was meant:

     immutable string[Test.Type] DESCRIPTION;

     shared static this()
     {
         DESCRIPTION = [Test.Type.One: "One!", Test.Type.Two: 
"It's Two...", Test.Type.Three: "And... Three!"];
     }

     struct Test
     {
         enum Type { One, Two, Three };
     }

See also:
https://forum.dlang.org/post/p941qc$43q$1@digitalmars.com


More information about the Digitalmars-d-learn mailing list