So how do you create an associative array with an immutable value, anyways?

FeepingCreature feepingcreature at gmail.com
Fri Oct 7 15:27:51 UTC 2022


On Friday, 7 October 2022 at 13:58:52 UTC, Steven Schveighoffer 
wrote:
>
> `require` should work, since it effectively only assigns a 
> value if it doesn't already exist.
>
> If it doesn't, then maybe it can be fixed so it does.
>
> We can't allow just general assignment to compile, as you may 
> have an already existing value.
>
> -Steve

Can confirm that `require` doesn't work at present:

```
void main() {
     immutable(int)[int] aa;
     aa.require(5, 5);
     assert(aa[5] == 5);
}
```

/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(3453): 
Error: cannot modify `immutable` expression `*p`
onlineapp.d(5): Error: template instance `object.require!(int, 
immutable(int))` error
instantiating

It's hard to see how you'd make `require` work but not 
`assocArray`, since `require` is not exactly a built-in feature 
either.


More information about the Digitalmars-d mailing list