Cast converts AA to rvalue?

Johan j at j.nl
Wed Aug 10 17:14:08 UTC 2022


On Wednesday, 10 August 2022 at 09:52:10 UTC, ag0aep6g wrote:
> On 10.08.22 10:20, Johan wrote:
>> ```
>> shared immutable int[int] aa;
>> void main () {
>>      // (cast()aa)[1] = 1; // works without immutable
>>      (*cast(int[int]*)(&aa))[1] = 1;
>> }
>> ```
>
> We have shared static constructors for that:
>
> shared static this()
> {
>     aa[1] = 1; /* no cast needed */
> }

But our code is not a toy example ;-)  (sorry for unnecessary 
snarky remark)

```
void f() {
     aa[1] = 1; // error
}
shared static this()
{
     f();
}
```

I had considered it, but discarded it... `f` is also a template 
in our code. Your remark made me check again, and the call chain 
is short, perhaps I'll convert `f` to a template mixin... 
Unfortunately doesn't work: "immutable variable `aa` 
initialization is not allowed in nested function `f`".


-Johan



More information about the Digitalmars-d-learn mailing list