#include #include #include forall( E, V | TypedEnum( E, V ) | { string str( V ); } ) // routine to format value}$ string format_enum( E e ) { return label( e ) + '(' + str( value( e ) ) + ')'; // "label( value )" } enum(size_t) RGB { Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF }; // string library has conversion from size_t to string struct color_code { int R, G, B; }; enum(color_code) Rainbow { Red = {255, 0, 0}, Orange = {255, 127, 0}, Yellow = {255, 255, 0}, Green = {0, 255, 0}, // ... }; string str( color_code cc ) with( cc ) { return str( R ) + ',' + str( G ) + ',' + str( B ); // "R,G,B" } enum Fruit { Apple, Banana, Cherry }; // C enum const char * label( Fruit f ) { static const char * labels[] = { "Apple", "Banana", "Cherry" }; return labels[f]; } int posn( Fruit f ) { return f; } int value( Fruit f ) { static const char values[] = { 'a', 'b', 'c' }; return values[f]; } string str( int f ) { string s = (char)f; return s; } int main() { sout | format_enum( RGB.Green ); // "Green(65280)"}$ sout | format_enum( Rainbow.Green ); // "Green(0,255,0)"}$ sout | format_enum( Cherry ); // "Cherry(c)" }