News for hackers. Stuff that matters.
Re:Java Secret: Using an enum to build a State machine by breckinloggins on Tuesday July 12, 2011 @12:12PM (#2755272)
The page crashed in Chrome, fyi (EDIT: once. I think it was an ad that was served).

Also, I'm hesitant to call anything regarding enums and State Machines a "secret", no matter the language. What IS cool about Java enums, though, is that they can encapsulate behavior. Building a state machine with C# or C enums involves defining the enum and then writing a giant switch statement (often in several places) or making a generic callback registration engine.

EDIT: Are there any other languages that do cool things with enums like this? I was delighted the first time I learned you could do this in Java.

One way around this if you're in, say, C#, is to just forget enums and use a static class...

  class State {
    static State Init = () -> // do stuff
    static State Running = () -> // do running stuff
    static State Close = () -> // do closing stuff
    
    static void NewState(State state)  {
      state()
    }
And you can reference the states just like enums:

  if (someState == State.Init) // then do something with it
(I know this isn't syntactically correct C#... I haven't used the language in over a year now.)
Re:Java Secret: Using an enum to build a State machine by icco on Tuesday July 12, 2011 @12:18PM (#2755301)
Page didn't crash in Chrome Beta on Linux.

And I agree, while good knowledge, I don't think this is a secret. I remember a simpler example of this being taught in some of my intro CS classes...

As for doing this in other languages, I think Java's ability to encapsulate is pretty unique... but I'm not sure. In theory you should be able to do something similar in the "pure" OO languages (Ruby, Python, SmallTalk, etc).

Re:Java Secret: Using an enum to build a State machine by gnaritas on Tuesday July 12, 2011 @01:03PM (#2755454)
FYI, Smalltalk is one word, not two crammed together.
Re:Java Secret: Using an enum to build a State machine by noblethrasher on Tuesday July 12, 2011 @01:44PM (#2755642)
Since a function is just an object with a single 'apply' method, I agree with the spirit of your code.

However, the concept is more naturally/idiomatically expressed in C# using an abstract class and variants (i.e. derived classes) for the various states.

This also gives you the advantage that you can encode valid state transitions in the type system itself.

Re:Java Secret: Using an enum to build a State machine by darklajid on Tuesday July 12, 2011 @04:06PM (#2756298)
No offense, in general I'm in agreement with your post.

But you say 'it's no secret' and admit a couple of lines later that you were 'delighted' when you first heard about this particular feature.

So - I'd like to give the author the benefit of the doubt. No, 'secret' is probably crap here, but it can be surprising and a nice revelation nevertheless.

Re:Java Secret: Using an enum to build a State machine by breckinloggins on Tuesday July 12, 2011 @06:12PM (#2756845)
I think it depends on how you read the "secret" part of the headline. If he means "here's a secret: use enums for state machines in Java", then it's no secret at all. If he meant "here's a secret for how to better implement state machines in Java", then yes, I would call it a secret.

As you might guess, I read it the first way :)

Re:Java Secret: Using an enum to build a State machine by peter_lawrey on Wednesday July 13, 2011 @10:19AM (#2759018)
The secret part is that enums are under utilised even though they have been around since 2004. I would assume that many people don't realise how theyc an be used. Its no secret they exist. ;)
Re:Java Secret: Using an enum to build a State machine by stcredzero on Tuesday July 12, 2011 @01:40PM (#2755620)
Are there any other languages that do cool things with enums like this?

Is there a similar trick for Go?