DIPX: Enum Literals / Implicit Selector Expression

bauss jj_1337 at live.dk
Fri Dec 3 09:36:46 UTC 2021


On Friday, 3 December 2021 at 09:35:47 UTC, bauss wrote:
> On Friday, 3 December 2021 at 04:58:51 UTC, Dave P. wrote:
>> On Friday, 3 December 2021 at 04:25:06 UTC, Elronnd wrote:
>>> On Friday, 3 December 2021 at 02:38:18 UTC, Dave P. wrote:
>>>> Where this really shines are with bit flags.
>>>
>>> Bitflags are a case where 'with' _does_ work.
>>>
>>> More generally, I'm sceptical of the feature for the reasons 
>>> Dennis and Paul mention; there is clearly some advantage wrt 
>>> boilerplate in some cases, but not for bitflags.
>>
>> Except that `with` introduces a new scope and can’t be used at 
>> file scope:
>>
>> ```d
>> enum Flags {
>>     FOO = 1,
>>     BAR = 2,
>>     BAZ = 4,
>>     QUX = 8,
>> }
>>
>> void main(){
>>     with(Flags){
>>         auto flags = FOO | BAR | BAZ | QUX;
>>     }
>>     // flags does not exist here.
>> }
>>
>> with(Flags){ // Illegal
>>     auto f = FOO | BAR;
>> }
>> ```
>>
>> With is also very noisy syntactically when all you want is to 
>> use enums in a nicer way.
>
> You could do this however:
>
> ```d
>     Flags flags;
>     with(Flags){
>         flags = FOO | BAR | BAZ | QUX;
>     }
>     // flags does exist here now.
> ```

Or even:

```d
     Flags flags;
     with(Flags) flags = FOO | BAR | BAZ | QUX;
     // flags does exist here now.
```


More information about the Digitalmars-d mailing list