I just read the niftiest new thing I've seen in a while. I've hated certain aspects of Java pretty much since it came out in the mid-90s, but for several years now we've known the feature list of Java 1.5, and it addressed my complaints, and threw in a few other nice things that I hadn't complained about, but appreciate the improvement.
So for maybe three years now, I've been raving about Java 1.5 despite not actually being able to write anything in it—it's been out quite a while now, but not on all platforms, and general adoption has been slow in some areas. Anyway, in helping a student decide how to implement a certain aspect of his project, I wanted to check out how Java 1.5 implemented the enumerated types I'd heard about.
They're so cool!
The basic enumerated type has been around for decades: in Pascal, for instance, you could say
type Month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec); var m: Month; m := May;and this was judged to be cleaner than saying "m := 5" because you couldn't mistake a Month for a Day or a Suit or anything else. However, the identifier May was still really only a fancy name for the number 5 (or perhaps 4); there wasn't anything you got out of it other than readability and type safety. In C, it was really nothing more than a fancy name for a number—you didn't even get type safety out of the bargain.
So I figured Java's enums were out of this mould, probably more like Pascal than C but still just slightly fancy numbers. No, no, no. Java enums are quite a high-level construct, in fact; while you can treat them like Pascal enums, they can also carry data and behaviours just like any other object. Because in Java, an "enumerated type" is just a class whose instances are all known at compile time. So if you have a limited list of things, like notes on a scale or planets in a solar system or items in a pull-down menu, regardless of how complex those objects are, you get to use an enumerated type.
And a few things come for free when you define an enum, like values() and toString(), which along with the "for-each" loop (another grand new construct in Java 1.5) lets you write stuff like
public enum Suit { SPADES, HEARTS, DIAMONDS, CLUBS } ... for (Suit s: Suit.values() ) { System.out.println (s); }and it will print out the names of each suit, in order. Nifty, eh?
The Joys of Finnish:
"Kokko, Kokoon koko kokko." ("Kokko, gather together the whole bonfire.")
"Kokoko kokko?" ("The whole bonfire?")
"Koko kokko, Kokko." ("The whole bonfire, Kokko.")
--Eric Dahlman
My goodness. You remember your Pascal? I haven't touched it since freshman year. Well nigh 19 years ago.
Heh. I think I am slipping into "old fart" mode. Using phrases like "well nigh".
Posted by ansible at 9:49am on 16 Jan 2006Greg: Is any of that really a benefit to C#? I think all the stuff you describe is from .NET (and you allude to this fact). And, quite independently of the fact that C# was developed by Microsoft, I believe it still remains the case that to seriously use it you're still locked in to the Microsoft hegemony. So, no.
Posted by blahedo at 1:46am on 19 Jan 2006