digitalmars.D.learn - Can Enums be integral types?
- Manfred Nowak (9/9) Apr 16 2022 In the specs(17) about enums the word "integral" has no match.
- Bastiaan Veelo (18/27) Apr 17 2022 Not sure where the question is, but
- Era Scarecrow (14/21) Apr 18 2022 The 'integral' or numeric value is used for uniqueness, not for
- Bastiaan Veelo (11/12) Apr 19 2022 There is nothing that requires enum values to be unique, though:
- Era Scarecrow (2/11) Apr 19 2022 True, but if you want it be useful they really need to be unique.
- Manfred Nowak (3/5) Apr 19 2022 Thy. That's the anchor in the specs preventing Enums to be
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
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
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: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.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.”
Apr 18 2022
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
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
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