digitalmars.D - Symbols
- bearophile (8/15) Jul 31 2008 The improvements to the type system, better multithreading support, etc,...
The improvements to the type system, better multithreading support, etc, help
program "in the large", but to allow the language to scale down you may enjoy
some simpler things too, that make code shorter, more bug free, easier to read,
etc. Such things may be less important than the big ones, but they too increase
the pleasure in using the language, etc.
In this part of the docs "Arrays that parallel an enum":
http://www.digitalmars.com/d/1.0/ctod.html#arrayenum
It shows:
The D Way
enum COLORS { red, blue, green }
string[COLORS.max + 1] cstring = [
COLORS.red : "red",
COLORS.blue : "blue",
COLORS.green : "green"];
Not perfect, but better.
I too have had to create an array of strings that parallels an enum, and I
think the D way is messy still (a mixin can be created to do this in a line of
code, but with this you end with two names anyway). In such situation I think
symbols may be much cleaner. Their toString gives the same name you use in the
code.
Symbols are present in Ruby and I think CLisp, and absent in Python.
Bye,
bearophile
Jul 31 2008








bearophile <bearophileHUGS lycos.com>