Can Enums be integral types?

Era Scarecrow rtcvb32 at yahoo.com
Tue Apr 19 01:25:13 UTC 2022


On Sunday, 17 April 2022 at 18:25:32 UTC, Bastiaan Veelo wrote:
> On Saturday, 16 April 2022 at 11:39:01 UTC, Manfred Nowak wrote:
>> In the specs(17) about enums the word "integral" has no match. 
>> But because the default basetype is `int`, which is an 
>> integral type, enums might be integral types whenever their 
>> basetype is an integral type.
>
> The reason is in [17.1.5](https://dlang.org/spec/enum.html):  
> “EnumBaseType types cannot be implicitly cast to an enum type.”

The 'integral' or numeric value is used for uniqueness, not for 
math or some other effect, anymore than a primary int key in a 
SQL database is used to identify someone's birthday. (*Maybe 
that's the wrong analogy, comparing apples to oranges perhaps*).

  We will indeed have to explicitly cast to get around it, though 
it doesn't mean much. If you have say true=1, blue=2, what is 
blue+true? Numerically it's 3 but there's no value 3, or value 3 
could be say potato...

  Few years ago i made an enum type flag/library storage library, 
which would take an int and convert to N flags, or N flags to an 
int for compactly storing said values. But it's been quite a 
while, though i do recall a lot of casting and binary 
AND/OR/XOR's involved for it to work the way it was intended.


More information about the Digitalmars-d-learn mailing list