PROPOSAL: Abstract enums (version 2)

Tim Peierls tim at peierls.net
Mon May 11 21:30:03 PDT 2009


It just doesn't seem worth the pain of a language change to me. If I were
ever really at a loss for a name for the singleton instance, I could go
minimal.
enum SingletonEnum {
    _;

    void method() { System.out.println("method called"); }

    public static void main(String... args) {
        SingletonEnum._.method();
    }
}

Or "THE", or "IT". But usually there's a natural name:

// Socratic singletons
enum Good { KNOWLEDGE; ... }
enum Evil { IGNORANCE; ... }

--tim


On Mon, May 11, 2009 at 10:41 PM, Howard Lovatt <howard.lovatt at iee.org>wrote:

> Tom Hawtin wrote:
> >> Yes a single element enum is a great singleton. Instead of:
> >
> > "Great singleton" is probably an oxymoron.
>
> I find a limited set of instances, enums, useful and the singleton is
> just the limit of that concept.
>
> >> enum Singleton {
> >>  // No INSTANCE
> >>  void method() { ... }
> >>  ...
> >> }
> >
> > I think for grammatical reasons you'll need to retain the semicolon.
>
> Can you expand on this, you are probably right if looked at the
> grammar closely but I can't see where the problem is from a quick look
> - I would be interested to know where the problem is.
>
> >> Usage:
> >>
> >> Singleton.method();
> >
> > If you add static to the method declaration, you are already there. Of
> couse still have the problem that you have introduced a load
> > of inappropriate methods via enum.
>
> I am wanting the enum to conform to an arbitrary, or at least near
> arbitrary, interface and/or abstract class. Therefore these need to be
> instance methods, not statics. As an aside, this is how Scala and
> Fortress are able to deal with statics in a strictly OO manor by
> having a singleton, which in the case of these two languages uses the
> object keyword to make exactly one instance of the class. I am
> proposing a similar solution for Java, but utilising an extension of
> the enum construct.
>
> You can't have a static and an instance method with the same
> signature, therefore I don't think the call Singleton.method() will be
> confusing to people or the compiler.
>
>  -- Howard.
>
>



More information about the coin-dev mailing list