enum for beginners

Johannes Totz johannes at jo-t.de
Tue Nov 15 13:47:06 PST 2011


On 15/11/2011 20:37, Mike Wey 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 constant 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?
>>
>>
>> Johannes
>
> It's a bug: string enums don't work with -g compiler switch.
>
> http://d.puremagic.com/issues/show_bug.cgi?id=5168

Ah thanks! I've added my case...

How would i go about using a custom class for the enum-type?
Various combinations of the snippet below lead to errors.

class EnumType
{
	int	x;
	alias x this;

	this(int i)
	{
		x = i;
	}

	void opCall(int i)
	{
	}
}

enum X : EnumType
{
	a = EnumType(1),
	b = 2
}


Errors range are always variations of

main.d(16): Error: function main.EnumType.opCall need 'this' to access 
member opCall
main.d(23):        called from here: opCall(1)
main.d(23): Error: cannot implicitly convert expression (opCall(1)) of 
type void to main.EnumType
main.d(24): Error: cannot implicitly convert expression (2) of type int 
to main.EnumType


More information about the Digitalmars-d-learn mailing list