Manifest constants - why not 'alias' ?

Bill Baxter dnewsgroup at billbaxter.com
Fri Dec 7 22:59:02 PST 2007


Bill Baxter wrote:
> Walter Bright wrote:

>> and the proposed new enum variation is just a syntactic shorthand for 
>> an anonymous enum with one member, 

The main reason for enum to exist and to be called "enum" in the first 
place is because of its behavior of automatically assigning numeric 
values to a list of symbols.  The purpose for enums originally in C was 
to provide symbolic sets.  Like:

    enum { DIAMOND, HEART, CLUB, SPADE };

It doesn't really matter what the values are.  And in fact in some 
languages like OCaml I think you can't even ask what the value of 
DIAMOND is.  It's just a symbol that compares equal with DIAMOND and 
unequal with HEART, CLUB, and SPADE.  And I seem to recall they are 
rather proud of that fact that they have "real" enumerated types.

Rather than further watering down the meaning of "enum" I think it would 
make more sense to scale it back to being a simple automatically 
numbered list of symbols.  And let alias take all the other uses.

// manifest constant (literal/constant alias)
alias int Foo = 10;

// Group of manifest constants (literal/constant alias)
alias {
    int Foo = 10;
    int Bar = 20;
}

// Alias for type
alias float_t = float;

// Alias for type (nod to C syntax for people converting code)
alias float float_t;

// Group of type aliases
alias {
     float_t = float;
     int_t = int;
     vector_t = vec!(int);
}

// module aliases
alias math = std.math;

// module aliases (legacy syntax. keep or phase out slowly)
alias std.math math;

// Mix n match!
alias {
     float_t = float;
     float PI = 3.14159;
     math = std.math;
}

They all have the common meaning of "let X be a new name for Y". 
Actually it becomes a lot like a safe version #define.

And then you could leave enums for the things where you really do want 
the automatic numbering behavior that it's named after.

enum Suit {
    HEART,DIAMOND,SPADE,CLUB
}

--bb



More information about the Digitalmars-d mailing list