DIP1044---"Enum Type Inference"---Formal Assessment

ryuukk_ ryuukk.dev at gmail.com
Thu May 11 00:56:03 UTC 2023


I was reading DMD source code, case of repetition that adds 
nothing of value but useless reading strain


https://github.com/dlang/dmd/blob/999d835c1196eb993a99bb7f1c863da265a6b6c0/compiler/src/dmd/json.d#L843-L869


```D
         if (target.os == Target.OS.Windows)
         {
             item("windows");
         }
         else
         {
             item("posix");
             if (target.os == Target.OS.linux)
                 item("linux");
             else if (target.os == Target.OS.OSX)
                 item("osx");
             else if (target.os == Target.OS.FreeBSD)
             {
                 item("freebsd");
                 item("bsd");
             }
             else if (target.os == Target.OS.OpenBSD)
             {
                 item("openbsd");
                 item("bsd");
             }
             else if (target.os == Target.OS.Solaris)
             {
                 item("solaris");
                 item("bsd");
             }
         }
         arrayEnd();
```

what could be done to avoid the repetition?


Reduce the length of the repetition with a short alias?

```D
         alias TOS = Target.OS;

         if (target.os == TOS.Windows)
         {
             item("windows");
         }
         else
         {
             item("posix");
             if (target.os == TOS.linux)
                 item("linux");
             else if (target.os == TOS.OSX)
                 item("osx");
             else if (target.os == TOS.FreeBSD)
             {
                 item("freebsd");
                 item("bsd");
             }
             else if (target.os == TOS.OpenBSD)
             {
                 item("openbsd");
                 item("bsd");
             }
             else if (target.os == TOS.Solaris)
             {
                 item("solaris");
                 item("bsd");
             }
         }
         arrayEnd();
```

Well, repetition is still there, now you got a short Type to type 
that doesn't mean anything without context, why is it needed? why 
do we need that noise? what problem does it solve?

We can use ``with()``? but why do i need a with here? target.os 
is self explanatory already, it's supposed to be type safe, it's 
a type safe enumeration, i should only care about the values, why 
now introduce an extra scope with extra indentation? it'd create 
more noise and now visual strain

Don't you find this code easier to read and review?

```D
         if (target.os == .Windows)
         {
             item("windows");
         }
         else
         {
             item("posix");
             if (target.os == .linux)
                 item("linux");
             else if (target.os == .OSX)
                 item("osx");
             else if (target.os == .FreeBSD)
             {
                 item("freebsd");
                 item("bsd");
             }
             else if (target.os == .OpenBSD)
             {
                 item("openbsd");
                 item("bsd");
             }
             else if (target.os == .Solaris)
             {
                 item("solaris");
                 item("bsd");
             }
         }
         arrayEnd();
```



More information about the Digitalmars-d-announce mailing list