www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string enums

reply "Lodo" <lodovico giaretart.net> writes:
Hi!
I'm having some troubles with an enum with base type string.
Here's the code:

enum Type:string
{
	DOT="DOT",
	ID="ID",
	MODULE="MODULE",
	SEMICOLON="SEMICOLON",
	ERROR="ERROR",
	EOF="EOF"
}

For every element of the enum, dmd writes this message two times:
  Integer constant expression expected instead of "DOT"
(with "ID", "MODULE", ... instead of "DOT")

Am I doing something wrong?

Thanks in advance.

       Lodo
Apr 27 2013
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
I can't help too much with your problem because I tried it and it 
works for me (on my dmd 2.061), but the pattern you're doing 
there is actually unneeded:

enum Type {
    DOT, ID, MODULE, /* etc ....*/
}

would actually work and you can get a string out of it with 
std.conv.to:

import std.conv;

Type t = Type.DOT;

string s = to!string(t);

assert(s == "DOT");
Apr 27 2013
prev sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 27 April 2013 at 18:34:24 UTC, Lodo wrote:
 Hi!
 I'm having some troubles with an enum with base type string.
 Here's the code:

 enum Type:string
 {
 	DOT="DOT",
 	ID="ID",
 	MODULE="MODULE",
 	SEMICOLON="SEMICOLON",
 	ERROR="ERROR",
 	EOF="EOF"
 }

 For every element of the enum, dmd writes this message two 
 times:
  Integer constant expression expected instead of "DOT"
 (with "ID", "MODULE", ... instead of "DOT")

 Am I doing something wrong?

 Thanks in advance.

       Lodo
Works fine for me with dmd >= 2.062
Apr 27 2013
parent reply "Lodo" <lodovico giaretart.net> writes:
On Saturday, 27 April 2013 at 18:41:45 UTC, Namespace wrote:
 Works fine for me with dmd >= 2.062
I'm using dmd 2.059. Maybe I will update it. I'm using MonoDevelop as IDE. I found that if I try to make an enum of doubles, dmd outputs 36 very complex error messages. I will try to investigate further.
Apr 27 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/27/2013 12:02 PM, Lodo wrote:

 I found that if I try to make an enum of doubles, dmd outputs 36 very
 complex error messages.
That works too. Tried with 2.063-devel-f6d55a9-dirty: enum Type : double { a = 1.5, b = 2.5 } void main() { auto e = Type.min; } Ali
Apr 27 2013