enum to string

Daniel Keep daniel.keep.lists at gmail.com
Tue Mar 10 18:55:11 PDT 2009



Nick Sabalausky wrote:
> Is there any way to do this (preferably in D1) with reflection? (ie, without 
> having to manually create a conversion func/lookup for every value of every 
> enum.)
> 
> ----------------------
> enum Shape
> {
>     Square, Circle
> }
> char[] foo(Shape s)
> {
>     // ?????
> }
> 
> // Either one of these, I don't really care which
> assert(foo(Shape.Square) == "Shape.Square");
> assert(foo(Shape.Square) == "Square");
> ----------------------

The only way that I know of is to generate foo at the same time as
Shape.  Generally, you write a CTFE function that generates the enum and
also generates the toString function.

  -- Daniel


More information about the Digitalmars-d-learn mailing list