Unable to use map() and array() inside a class-field's initializer.

realhet real_het at hotmail.com
Thu Jul 14 13:57:24 UTC 2022


Hello,

Somehow it can't reach map and array inside a class field 
initializer. If I put that small expression inside a function, it 
works. If I encapsulate the initializer expression into a lambda 
and evaluate it right away, it also works. Only the nice form 
fails.

Why is that?

```d
import std;

enum E{a, b, c}

static struct S{
     const E e;
     string otherProperties;
}

//trying to initialize an array inside

static if(1) class D{
   //this fails: Error: function `onlineapp.D.map!(E[]).map` need 
`this` to access member `map`
   auto x = [EnumMembers!E].map!(e => S(e)).array;
}

auto initialS(){
   return [EnumMembers!E].map!(e => S(e)).array;
}

class C{
   auto x = initialS; //this way it works
}

void main(){
     writeln((new C).x);
}
```



More information about the Digitalmars-d-learn mailing list