Typesafe Enums.
JavaWorld is running another series on enums; yes, this issue has been covered to death in the press over the years. Here's my take -- enums in Java are a pain in the ass to do. Why don't we just take a quick look at what's out there, and what works?
The best model I've seen is what Delphi/Pascal do. You can declare enumerations with contants. They are typesafe. Then, importantly, you can declare sets of them. Something like this:
myEnum = (meFirst, meSecond, meThird);
myEnumSet = set of myEnum;
Handling sets of enums (and set operations) is something that happens all the time. It's something that Pascal got completely right, way back when, and hasn't been done right in any of the major languages since. Please, for the love of god, put proper enums and sets in the language. They'll get used all the time, and the resulting code is wonderfully readable.
Sometimes I miss Delphi!
10:52:53 AM
|