Discussion Thread: DIP 1044--Enum Type Inference--Community Review Round 1

ryuukk_ ryuukk.dev at gmail.com
Wed Nov 23 13:41:12 UTC 2022


On Wednesday, 23 November 2022 at 11:57:48 UTC, deadalnix wrote:
> On Tuesday, 22 November 2022 at 21:44:00 UTC, XavierAP wrote:
>> On Friday, 18 November 2022 at 18:54:01 UTC, monkyyy wrote:
>>>
>>> Ds enum syntax is verbose compared to c; and the code block 
>>> probaly isnt possible to write
>>
>> I know I'm not teaching anything that's not known, but 
>> languages such as D, modern C++, C# etc requiring enums to be 
>> qualified, was absolutely intentional. And IMHO the reasons 
>> (against the C syntax) stand.
>
> And wait, there is more: we have anonymous enums:
>
> ```d
> enum { A, B }
> ```
>
> For these who are allergic to named enums.


```D
// verbose API, readable
struct SuperLongStruct
{
     struct SuperLongInnerStruct
     {
         enum SuperLongEnum
         {
             VALUE_A, VALUE_B
         }

         SuperLongEnum super_long_flags;
     }
     SuperLongInnerStruct some_data;
}

// oh shoot

SuperLongStruct super_long_struct = {
     some_data: {
         super_long_flags:  
SuperLongStruct.SuperLongInnerStruct.SuperLongEnum.VALUE_A | 
SuperLongStruct.SuperLongInnerStruct.SuperLongEnum.VALUE_A
     }
};

// oh nice!

SuperLongStruct super_long_struct = {
     some_data: {
         super_long_flags: .VALUE_A | .VALUE_A
     }
};

```

no need to introduce noise, no need to care about an alias that 
is leaking, the usage of the API is simple and expressive, i'm 
not sacrificing the API with simpler and meaningless names

i make fully use of scoped types in D, and i avoid the noisy 
verbosity that comes with it as a result

Adam's idea to use `auto` could be the proper compromise

It's not about what other languages do, it's about how it helps 
the user avoid making their code unreadable


```D
MyType value = MyType.some;

```

is `.some` a static function? does it return something? is it a 
static variable? an enum? you don't know!

so the argument that it Enum Type Inference reduce readability is 
not valid, if your API is not readable, that's the problem

Helping users be more expressive and less verbose helps them keep 
and maintain proper naming for their types/variables, wich helps 
readability


```D
AttackType attack_type = .MELEE;


(..)


switch (attack_type)
{
     case .MELEE:
     break;
}



```

now your variable name carry more and proper information about 
what it is,


More information about the Digitalmars-d mailing list