Re: Java generics limitations?



seanwinship@xxxxxxxxx wrote:
I'm trying to use Java's generics to implement a concept that is
straightforward with C++'s templates.  The underlying idea is known as
the Curiously Recurring Template Idiom, first described, as far as I
know, by James Coplien.  I've used it in C++ to encapsulate generic
behavior like the Singleton pattern:

template <class T> class Singleton
{
private:
  static T instance_;

public:
  static T& instance() { return instance_; }
};

Using this template, creating a Singleton is simple:

class Foo : public Singleton<Foo>
{
  . . .
};

Why not:

public final class Foo {
    private static final Foo instance = new Foo();
    public static Foo getInstance() {
        return instance;
    }
    private Foo() {
        ;
    }
    ...
}

Doesn't seem too difficult. If you don't like all the typing and don't need a particular base class implementation:

public enum Foo {
    INSTANCE;
    ...
}

Other than enums, I hope you don't have too many singletons.

I'm coming to the conclusion that generics in Java are basically
worthless except as a means of eliminating the need to do some casting
when using collections.  Are they really so limited or am I missing
something?

You have to remember that Java is not C++. Generics are not templates. Just because some random hack works in C++ doesn't mean it should in Java. Bashing your head against the wall will just produce poor code.


Java generics are designed to express in the language certain constraints on types objects interact with. A static equivalent of polymorphism. That they do that superbly. IMHO, for the job they are designed to do they wipe the floor with C++ templates.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.



Relevant Pages

  • Re: faster access private or public?
    ... > void f ... Whoops, Foo should have had: ... private, and make the original function a friend of Foo. ... Languages like Java have much more ...
    (microsoft.public.vc.language)
  • Re: Java Instanz und Objekt frage ?
    ... >>>fully qualified name der Klasse.Wenn du jetzt diesen Namensraum nimmst ... > laut VM ist der FQN von Foo Foo und nicht .Foo. .Foo ist ein ungültiger ... einem gedankenexperiment die Sprache Java' vorzustellen, ... Nochmal: die Transformation geht so: ...
    (de.comp.lang.java)
  • Re: static class inheritance, generalized
    ... private Bar _bar; ... private string _val; ... private Foo _container; ... horrible the more readability and clarity of the code goes out the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: On Symbols
    ... Have any of the explanations in the thread (What is the difference between:foo and "foo" ?) helped you understand symbols? ... For example, a Lisp symbol can also be bound to a value, a property list, or a function. ... I do know what a string is, and the definition of "interned" is supplied. ... I don't know Java. ...
    (comp.lang.ruby)
  • Re: CLOS and databases
    ... > define macros that can use type information (something that Common ... in my interpreter I've used the Java bean conventions to map accessors, ... It only tries getFoo() and isFooif foois undefined. ... either the handler for the signalling thread is null or the no handler ...
    (comp.lang.lisp)