Constants, Aliases
Xinok
xnknet at gmail.com
Wed Dec 13 23:00:15 PST 2006
In C++, you can define a constant by using a preprocessor:
#define INT 30
In D, the standard is to use 'const':
const int INT = 30;
The problem with the const keyword is it doesn't guarantee the expression will
be constant.
const int* PTR = new int; // This technically isn't a constant, the compiler
just doesn't allow you to modify it's value.
Another solution is to use enumerators, but they don't aloow any type other
than int:
enum { val = 30 } // OK
enum { str = "Hello" } // Error
So I was thinking, why not give this job to aliases? Aliases must be constant,
you can use any type with them, and they're easy to write.
alias 30 VAL;
alias "Hello" STR;
alias 31.5 DEC;
Expressions can simply be put within parenthesis:
alias (15 * 99) EXP;
More information about the Digitalmars-d
mailing list