is there a way to use sumtype in `switch/case` (with multiple statements)? or how can I get the `tag` and `storage`?

mw m at g.c
Sat Oct 7 19:25:51 UTC 2023


https://dlang.org/library/std/sumtype.html

seems right now the `match!(...)` template only generate a 
delegate, e.g. suppose the following (silly) code:

```
bool isF(Temperature t) {
   while (true) {
     t.match!(
       (Fahrenheit f) {return true;},
       (_) {return false;}  // I want to return from the func isF, 
not the delegate!
     );
   }
}

enforce( isF(t1));
```

currently, this code will do infinite loop.


I'm wondering if the following are possible:

```
bool isF(Temperature t) {
   while (true) {
     switch (t) {
       case Fahrenheit f: /* do multiple statements of `f` */ 
return true;
       default: return false;
     }
   }
}
```

Or how can I get the `tag` and `storage` myself?

https://github.com/dlang/phobos/blob/a3f22129dd2a134338ca02b79ff0de242d7f016e/std/sumtype.d#L310

Thanks.


More information about the Digitalmars-d-learn mailing list