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

Paul Backus snarwin at gmail.com
Thu May 11 03:22:03 UTC 2023


On Thursday, 11 May 2023 at 00:56:03 UTC, ryuukk_ wrote:
> 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();
> ```

Honestly, not really. I've never looked at this part of DMD, and 
without context, I'd have no idea where the symbols `Windows`, 
`linux`, `FreeBSD`, and so on were coming from. Having it 
explicitly spelled out at the usage site--either via qualified 
names (`Target.OS.Windows`) or a local alias (`alias TOS = 
Target.OS`) makes it trivial to understand what's being referred 
to.

In fact, for this particular example, there are actually two 
enums in the DMD source code that these symbols could be coming 
from: `enum TargetOS` in `cli.d`, and `enum OS` in `target.d`. So 
you would have to scroll up and look at the imports to 
disambiguate.


More information about the Digitalmars-d-announce mailing list