www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - do something with bitfields and switch statements

reply monkyyy <crazymonkyyy gmail.com> writes:
https://forum.dlang.org/thread/fsjfcairmfcdwraxabdk forum.dlang.org

if bitfields are leaving preview and currently doing nothing 
special with switch

```d
struct foo{
	bool a:1;
	bool b:1;
	bool c:1;
	uint i:29;
}
unittest{
	foo bar=foo(false,false,false,1337);
	switch(bar,3){
		case 0: bar.i.writeln; break;
		default:"bye".writeln; break;
	}
}
```

maybe allow switch to take 2 parameters, if the first is a struct 
with bit fields, the 2nd is the number of bits it bitshifts off 
the top
Aug 06
parent reply Nick Treleaven <nick geany.org> writes:
On Wednesday, 6 August 2025 at 19:00:09 UTC, monkyyy wrote:
 https://forum.dlang.org/thread/fsjfcairmfcdwraxabdk forum.dlang.org

 if bitfields are leaving preview and currently doing nothing 
 special with switch

 ```d
 struct foo{
 	bool a:1;
 	bool b:1;
 	bool c:1;
 	uint i:29;
 }
 unittest{
 	foo bar=foo(false,false,false,1337);
 	switch(bar,3){
 		case 0: bar.i.writeln; break;
 		default:"bye".writeln; break;
 	}
 }
 ```

 maybe allow switch to take 2 parameters, if the first is a 
 struct with bit fields, the 2nd is the number of bits it 
 bitshifts off the top
That doesn't seem to fit as part of the remit of `switch`, what if there's another field after `i`? Why is it better than `switch (bar.i)`?
Aug 07
parent monkyyy <crazymonkyyy gmail.com> writes:
On Thursday, 7 August 2025 at 16:35:45 UTC, Nick Treleaven wrote:
 On Wednesday, 6 August 2025 at 19:00:09 UTC, monkyyy wrote:
 [...]
That doesn't seem to fit as part of the remit of `switch`, what if there's another field after `i`? Why is it better than `switch (bar.i)`?
Its not bar.i
 [...]
its `(bar.a<<2 | bar.b<<1 |bar.c)` in a single op that uses no introspection on the contents of bar
Aug 07