Changes before 1.0

Bill Baxter dnewsgroup at billbaxter.com
Sun Dec 3 15:42:05 PST 2006


janderson wrote:
>> It would be helpful if the following code would work:
>>     enum TestEnum {
>>         Value1, Value2
>>     }
>>     writefln("%s", TestEnum.Value2);
> 
> It would be even more helpful if a .ToString() for enums was added.  The 
> compiler could detect when it was used and only add those particular 
> enums to the binary.  
 > It would be most useful in scripting where you
> want to convert a string to an enum.  Which leads on to having a way of 
> converting a string to an enum directly.
> 
> string myenum = "Value1";
> TestEnum result = myenum;
> 
> Ok getting its a bit complicated here and probably not worth the effort 
> however I deal with this type of code at least once a week.  Maybe that 
> would be a 2.0 feature.


I agree that I've wanted this kind of feature before too, but I don't 
want it at the expense of bloating every single library that contains 
enums with lots of string equivalents that will never get used in real 
code.  I.e. this kind of thing is really only useful for debug or 
prototype code.

But I think you're right, the compiler has to have access to the enum 
names in order to compile any code that uses the enums, and if it does 
then it should be able to replace something like enumval.str with the 
literal string value.  So it would act more like a macro expanded at 
compile time than an actual function.

--bb



More information about the Digitalmars-d mailing list