enum for beginners

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 15 07:55:44 PST 2011


On Tue, 15 Nov 2011 10:47:22 -0500, Johannes Totz <johannes at jo-t.de> wrote:

> On 15/11/2011 15:43, Johannes Totz wrote:
>> On 14/11/2011 22:32, Timon Gehr wrote:
>>> On 11/14/2011 11:25 PM, Johannes Totz wrote:
>>>> Hi!
>>>>
>>>> I'm having trouble with named typed enums.
>>>> This works (unnamed):
>>>>
>>>> enum : string
>>>> {
>>>> a = "a",
>>>> b = "b"
>>>> }
>>>>
>>>> int main(string[] argv)
>>>> {
>>>> writeln(a);
>>>> return 0;
>>>> }
>>>>
>>>>
>>>> But this does not:
>>>>
>>>> enum X : string
>>>> {
>>>> a = "a", // Error: Integer constan
>>>
>>> t expression expected
>>>> // instead of "a"
>>>> b = "b" // Error: Integer constant expression expected
>>>> // instead of "b"
>>>> }
>>>>
>>>> int main(string[] argv)
>>>> {
>>>> writeln(X.a);
>>>> return 0;
>>>> }
>>>>
>>>>
>>>> What did I miss?
>>>>
>>>
>>> I don't know. It works for me.
>>>
>>> import std.stdio;
>>>
>>> enum X : string{
>>> a = "a",
>>> b = "b",
>>> }
>>>
>>>
>>> int main(string[] argv) {
>>> writeln(X.a);
>>> return 0;
>>> }
>>>
>>> compiles, runs and prints "a" as expected.
>>>
>>> Are you using the latest version of the compiler? (DMD v2.056)
>>
>> I had 2.055 and just upgraded.
>> But this seems to be some issue with VisualD. Compiling on the command
>> line (with 2.056) works fine as expected.
>
> Ah, when I compile on the command line with:
>
> dmd -g -debug main.d
>
> main.d(6): Error: enum main.X base type must be of integral type, not  
> char[]
> main.d(8): Error: cannot implicitly convert expression ("a") of type  
> char[1u] to int
> main.d(9): Error: cannot implicitly convert expression ("b") of type  
> char[1u] to int

dmd must map to a D1 version, where string literals were char[N], not  
immutable(char)[].  In D1, you could not have enums that were strings.

-Steve


More information about the Digitalmars-d-learn mailing list