AA and struct with const member

tsbockman thomas.bockman at gmail.com
Tue Dec 28 10:02:13 UTC 2021


On Tuesday, 28 December 2021 at 07:54:56 UTC, frame wrote:
> On Tuesday, 28 December 2021 at 06:38:03 UTC, Tejas wrote:
>> The workaround is okay, but I think we should file a bug 
>> report for this.
>> This is very ~~stupid~~ undesirable behaviour
>
> I agree. I'll just wait if somebody can explain why this isn't 
> a bug or wanted behaviour or a known issue.

[The spec 
says](https://dlang.org/spec/hash-map.html#construction_assignment_entries):
```
2. If the assigned value type is equivalent with the AA element 
type:

     1. If the indexing key does not yet exist in AA, a new AA 
entry will be allocated, and it will be initialized with the 
assigned value.
     2. If the indexing key already exists in the AA, the setting 
runs normal assignment.
```
Thus, when the value type is constructable but not assignable:
```d
struct S
{
   const(int) a;
}

void test(S[string] aa, string key, int value)
{
   // Should be a compile-time error, because it might reassign:
   test[key] = S(value);

   // Should be accepted, because they can be proven at compile 
time to never reassign:
   test.require("a", S(value));
   test.update("a", () => S(value), (ref const(S)) => { });
}
```

`require` and `update` can be fixed rather easily in `object.d`; 
I have submitted [issue 
22633](https://issues.dlang.org/show_bug.cgi?id=22633) with 
sample code.


More information about the Digitalmars-d-learn mailing list