bool concat patternmatching with switch
monkyyy
crazymonkyyy at gmail.com
Thu May 30 17:11:56 UTC 2024
On Thursday, 30 May 2024 at 12:56:40 UTC, Salih Dincer wrote:
> On Tuesday, 28 May 2024 at 19:39:45 UTC, monkyyy wrote:
>> ```d
>> switch(i%3==0,i%5==0){
>> case 1,1: return "fizzbuzz";
>> case 0,1: return "buzz";
>> case 1,0: return "fizz";
>> case ?,?: return i.to!string;
>> }
>> ```
>
> Maybe smarter solutions can be produced with BitFlags. At least
> it will be more readable...
>
> ```d
> import std.stdio;
> import std.typecons;
>
> enum Status
> {
> Idle = 1 << 0, // 0001
> Running = 1 << 1, // 0010
> Paused = 1 << 2, // 0100
> Stopped = 1 << 3 // 1000
> }
>
> void main()
> {
> BitFlags!Status currentStatus;
> currentStatus = Status.Running;
> switch (currentStatus)
> {
> case Status.Idle:
> writeln("Durum: Idle");
> break;
> case Status.Running:
> writeln("Durum: Running");
> break;
> case Status.Paused:
> writeln("Durum: Paused");
> break;
> case Status.Stopped:
> writeln("Durum: Stopped");
> break;
> default:
> writeln("Bilinmeyen durum");
> break;
> }
>
> currentStatus = Status.Paused;
>
> switch (currentStatus)
> {
> case Status.Idle:
> writeln("Durum: Idle");
> break;
> case Status.Running:
> writeln("Durum: Running");
> break;
> case Status.Paused:
> writeln("Durum: Paused");
> break;
> case Status.Stopped:
> writeln("Durum: Stopped");
> break;
> default:
> writeln("Bilinmeyen durum");
> break;
> }
> }
> ```
>
> SDB at 79
your example doesn't show overlapping states and im confused why
you set up bitshifting without using it
switching over an enum I believe is solved with a `with` block;
going with the fizz buzz example, an `enum isfizz{true,false}`
wouldnt provide clarity
do try to take your example as something that show overlaping
states
```d
```d
nullable(T){
T me; alias me this;
bool isnull=false;
}
bool isvalid(USERNAME);//looks up a string in a database
bool authcheck(PASSWORD,USERNAME);//checks if user/pass match
switch(status,username.isnull,username.isvalid,password.isnull,password.authcheck(username){
with(Status){
case Running,0,1,0,1://ideal path
case ? ,0,1,0,1://reset server
case Running,1,?,?,?://show guest account
case ? ,?,?,?,0://invalid password page
}}
```
More information about the dip.ideas
mailing list