www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using 'with'

reply Vitaliy Fadeev <vital.fadeev gmail.com> writes:
Hi! Did we know you could do this?
To make the `enum` inside the `case` easier to read:

```D
void
func (Event* evt) {
     with (Event.Type)
     switch (evt.type) {
         case SET_E_PROP  : _set_e_prop (evt); break;
         case UPDATE_W    : _update_w   (evt); break;
         case UPDATE_H    : _update_h   (evt); break;
         case UPDATE_XY   : _update_xy  (evt); break;
         default:
     }
}

struct
Event {
union {
     Type type;

     enum
     Type {
         CLICK      =  1,
         UPDATE,
         SET_E_PROP,
         UPDATE_W,
         UPDATE_H,
         UPDATE_XY,
     }
}

```

Check the **with (Event.Type)**.
Jan 12
parent reply user1234 <user1234 12.de> writes:
On Monday, 12 January 2026 at 10:55:34 UTC, Vitaliy Fadeev wrote:
 Hi! Did we know you could do this?
 To make the `enum` inside the `case` easier to read:

 [...]

 Check the **with (Event.Type)**.
Yes this is a well known idiom, see https://p0nce.github.io/d-idioms/#Qualified-switch-using-with.
Jan 12
parent reply Vitaliy Fadeev <vital.fadeev gmail.com> writes:
On Monday, 12 January 2026 at 11:28:01 UTC, user1234 wrote:
 On Monday, 12 January 2026 at 10:55:34 UTC, Vitaliy Fadeev 
 wrote:
 Hi! Did we know you could do this?
 To make the `enum` inside the `case` easier to read:

 [...]

 Check the **with (Event.Type)**.
Yes this is a well known idiom, see https://p0nce.github.io/d-idioms/#Qualified-switch-using-with.
Wow! This looks great! Thanks!
Jan 12
parent reply user1234 <user1234 12.de> writes:
On Monday, 12 January 2026 at 11:40:07 UTC, Vitaliy Fadeev wrote:
 On Monday, 12 January 2026 at 11:28:01 UTC, user1234 wrote:
 On Monday, 12 January 2026 at 10:55:34 UTC, Vitaliy Fadeev 
 wrote:
 Hi! Did we know you could do this?
 To make the `enum` inside the `case` easier to read:

 [...]

 Check the **with (Event.Type)**.
Yes this is a well known idiom, see https://p0nce.github.io/d-idioms/#Qualified-switch-using-with.
Wow! This looks great! Thanks!
To go further, while this trick is great, there's been discussions [last years](https://forum.dlang.org/thread/zbugncpaooowjsxldzue forum.dlang.org) about _Parent Enum Inference_. Instead of ``` bitset |= 1 << EnumType.enumerator; ``` people want to the language to allow writing ``` bitset |= 1 << .enumerator; // EnumType can be infered from the OrAssign LHS ``` in this case `with` is of no help because it introduces a scope, because it's not an expression, and finally because it's only used once.
Jan 12
parent reply Nick Treleaven <nick geany.org> writes:
On Monday, 12 January 2026 at 11:58:01 UTC, user1234 wrote:
 To go further, while this trick is great, there's been 
 discussions [last 
 years](https://forum.dlang.org/thread/zbugncpaooowjsxldzue forum.dlang.org)
about _Parent Enum Inference_. Instead of

 ```
 bitset |= 1 << EnumType.enumerator;
 ```

 people want to the language to allow writing

 ```
 bitset |= 1 << .enumerator;  // EnumType can be infered from 
 the OrAssign LHS
 ```
How can it be inferred? There's no expected type for the right-hand side of the `<<` operator. Of course the link has cases that could be inferred.
 in this case `with` is of no help because it introduces a 
 scope, because it's not an expression, and finally because it's 
 only used once.
Jan 12
parent user1234 <user1234 12.de> writes:
On Monday, 12 January 2026 at 18:20:37 UTC, Nick Treleaven wrote:
 On Monday, 12 January 2026 at 11:58:01 UTC, user1234 wrote:
 To go further, while this trick is great, there's been 
 discussions [last 
 years](https://forum.dlang.org/thread/zbugncpaooowjsxldzue forum.dlang.org)
about _Parent Enum Inference_. Instead of

 ```
 bitset |= 1 << EnumType.enumerator;
 ```

 people want to the language to allow writing

 ```
 bitset |= 1 << .enumerator;  // EnumType can be infered from 
 the OrAssign LHS
 ```
How can it be inferred? There's no expected type for the right-hand side of the `<<` operator. Of course the link has cases that could be inferred.
 in this case `with` is of no help because it introduces a 
 scope, because it's not an expression, and finally because 
 it's only used once.
Yeah you're right with that remark... better example ``` bitset |= .enumerator ``` assuming the C style with the pre-shifted value defined for each enumerator.
Jan 12