www.digitalmars.com         C & C++   DMDScript  

D - Stringizing constant names

reply Kent Quirk <Kent_member pathlink.com> writes:
I've been looking through the D documents again recently, and I have a few
questions that I haven't located in the newsgroup. I'll post them as separate
documents to keep threading and subject lines relevant.

With no preprocessor (something I generally approve of), there's no stringize
mechanism. In the list of preprocessor idioms that are accounted for in other
ways, I didn't see anything about stringizing or printing the values of enums in
symbolic (text) form rather than as a number.

If I have X as an enum and print out X, I'd like to have it print as its enum
rather than as an integer.

Any hope for some help with this (in my experience) common problem?

- Kent
Jan 13 2003
next sibling parent reply Mark Evans <Mark_member pathlink.com> writes:
I second this desire (without arguing for a preprocessor).  All that
distinguishes an enum from a simple integer is the string attached, and there
should be some programmatic way to access that.

Mark
Jan 13 2003
parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
I believe that D supports that with the .toString() property.

Or it should.

enum colors { red, green, blue }
printf("%*s", colors.red.toString()); // should print "red"

If you want the integral value as a string, cast to int first.

char[] rednumstr = cast(int)(colors.red).toString();

Sean

"Mark Evans" <Mark_member pathlink.com> wrote in message
news:b00468$fl6$1 digitaldaemon.com...
 I second this desire (without arguing for a preprocessor).  All that
 distinguishes an enum from a simple integer is the string attached, and
there
 should be some programmatic way to access that.

 Mark
Jan 14 2003
prev sibling parent "Kelvin Lee" <kiyolee *hongkong*.com> writes:
In general, while using any programming language, it does not prohibit you
to preprocess your source code with whatever preprocessor, e.g. sed, perl,
cpp, you like before compiling the source code.

I suppose you may simply use the C preprocessor to process the source code
before compiling the source code with the D compiler.

I agree with the D language designer that preprocessing should not be part
of any programming language. And that means if you use any kind of
preprocessing, be careful about all the potential problems.

Kiyo

"Kent Quirk" <Kent_member pathlink.com> wrote in message
news:b0016h$dm0$1 digitaldaemon.com...
 I've been looking through the D documents again recently, and I have a few
 questions that I haven't located in the newsgroup. I'll post them as
separate
 documents to keep threading and subject lines relevant.

 With no preprocessor (something I generally approve of), there's no
stringize
 mechanism. In the list of preprocessor idioms that are accounted for in
other
 ways, I didn't see anything about stringizing or printing the values of
enums in
 symbolic (text) form rather than as a number.

 If I have X as an enum and print out X, I'd like to have it print as its
enum
 rather than as an integer.

 Any hope for some help with this (in my experience) common problem?

 - Kent
Jan 13 2003