Changes before 1.0

janderson askme at me.com
Mon Dec 4 21:45:18 PST 2006


Bill Baxter wrote:
 > janderson wrote:
 >> Bill Baxter wrote:
 >>> janderson wrote:
 >>>>> It would be helpful if the following code would work:
 >>>>>     enum TestEnum {
 >>>>>         Value1, Value2
 >>>>>     }
 >>>>>     writefln("%s", TestEnum.Value2);
 >>>
 >>> 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.
 >>> --bb
 >>
 >> I don't agree on this point (that it would only be used in debug).  As
 >> I said, in scripting  (ie game ai ect...) and in plugin libraries (ie
 >> maya) its common place to use a string lookup for enums.  The code
 >> isn't *much* longer or more bug pron
 >
 > Mmmm -- bug pr0n.  *much* longer ... more bug pr0n....  :-)

sorry I ment: slightly more bug prone but not much longer...

//enum -> string

enum A
{
   Value1,
...
};

string A[] =
{
   "Value1",
...
};


//string to enum

A AToString[char[]];

void register()
{
   AToString["Value1"] = Value;
   //ect...
}


Really not that much code.  Also a lot of times in C++ you register lots 
of things together...

struct AInfo
{
    string name;
    string discription;
    int cost;
};

string A[] =
{
   { "Value1", "Some value I picked", 10 },
...
};


 >
 >> to write unless you go the other way and want it to be efficient (ie
 >> string->enum is more complex then enum->string).  Still both cases are
 >> pretty trivial to write in code.
 >>
 >> I guess the main thing going for it is that users would recognize the
 >> pattern much more quickly (ie readability) although it does reduce the
 >> chance of offset errors (had one of those just the other day).
 >>
 >> -Joel
 >
 > Useful for bridging with scripting code. That's a good point.  Python
 > has no enums either, so they sometimes get turned into strings when
 > going into Python land.
 >
 > Hey Kirk if you're listening, what does PyD do with enums?
 >
 > --bb



More information about the Digitalmars-d mailing list