Stop using the enum as a manifest constant.

Q. Schroll qs.il.paperinik at gmail.com
Fri Jul 9 01:04:15 UTC 2021


On Sunday, 6 June 2021 at 15:05:47 UTC, Mathias LANG wrote:
>> Mutable enum???
>> It's absolutely unacceptable.
>>
>> ```d
>>     WAT[0] = 'w';
>> }
>> ```
> However, the compiler shouldn't allow you to use an enum as an 
> lvalue.

It doesn't, at least when taking the statement by word. The thing 
being an lvalue here is `WAT[0]`, not `WAT`. It gets clearer when 
a function `f` returns an array (by value, of course); then, 
`f()[0]` is an lvalue, despite `f()` not being one. Dynamic array 
elements always have an address, they're always lvalues. The 
statement `[1, 2, 3][1] = 2;` type-checks, compiles (as it 
should, albeit nonsensical) -- and considering that enums are 
named literals, `WAT[1] = 2;` isn't much different.

There's nothing stopping you from typing an enum `immutable`:
```D
enum immutable(int[]) WAT = [1, 2, 3];
```
Will refuse to have its elements assigned by virtue of 
`immutable`.


More information about the Digitalmars-d mailing list