AA and struct with const member

Stanislav Blinov stanislav.blinov at gmail.com
Wed Dec 29 01:11:13 UTC 2021


On Tuesday, 28 December 2021 at 22:46:16 UTC, frame wrote:
> On Tuesday, 28 December 2021 at 10:02:13 UTC, tsbockman wrote:
>
>>   // Should be a compile-time error, because it might reassign:
>>   test[key] = S(value);
>
> This might be a typo in your example but why should it be a 
> compile-time error, it cannot know if the key already exists in 
> compile time on a variable. First time initialization should 
> always work anyway.

Because opIndexAssign cannot distinguish at compile time between 
initialization and assignment:

```d
Stuff[Key] aa;
aa[key] = Stuff(args); // ostensibly, initialization
aa[key] = otherStuff;  // assignment to existing value
```

Same syntax, different behavior. This can only be caught at 
runtime. `require` and `update` though should be able to pull 
this off, and that they don't is a bug.


More information about the Digitalmars-d-learn mailing list