www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can Enums be integral types?

reply Manfred Nowak <svv1999 hotmail.com> writes:
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.

On the other hand the specs(7.6.5.3) about types say
| A bool value can be implicitly converted to any integral type,
| with false becoming 0 and true becoming 1.

This seems senseless, when the enum has no names defined for one 
of these values.
Apr 16 2022
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
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.

 On the other hand the specs(7.6.5.3) about types say
 | A bool value can be implicitly converted to any integral type,
 | with false becoming 0 and true becoming 1.

 This seems senseless, when the enum has no names defined for 
 one of these values.
Not sure where the question is, but [6.5.3](https://dlang.org/spec/type.html#bool) makes that this works: ```d int i = true; // 1 ``` However this does not: ```d enum E : int {Zero, One, Two} E e1 = true; // Error: cannot implicitly convert expression `true` of type `bool` to `E` E e2 = 1; // Error: cannot implicitly convert expression `1` of type `int` to `E` ``` The reason is in [17.1.5](https://dlang.org/spec/enum.html): “EnumBaseType types cannot be implicitly cast to an enum type.” — Bastiaan.
Apr 17 2022
next sibling parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
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.
Apr 18 2022
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Tuesday, 19 April 2022 at 01:25:13 UTC, Era Scarecrow wrote:
 The 'integral' or numeric value is used for uniqueness, […]
There is nothing that requires enum values to be unique, though: ```d import std; void main() { enum E {Zero = 0, One = 0, Two = 0} writeln(E.Two); // Zero! } ``` — Bastiaan.
Apr 19 2022
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Tuesday, 19 April 2022 at 13:20:21 UTC, Bastiaan Veelo wrote:
 There is nothing that requires enum values to be unique, though:
 ```d
 import std;
 void main()
 {
     enum E {Zero = 0, One = 0, Two = 0}
     writeln(E.Two); // Zero!
 }
 ```
True, but if you want it be useful they really need to be unique.
Apr 19 2022
prev sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
On Sunday, 17 April 2022 at 18:25:32 UTC, Bastiaan Veelo wrote:
 The reason is in [17.1.5](https://dlang.org/spec/enum.html): 
 “EnumBaseType types cannot be implicitly cast to an enum type.”
Thy. That's the anchor in the specs preventing Enums to be integral types.
Apr 19 2022