visionset schreef:
>>> Yes, but how do I now use this class to form a generic getter outside of
>>> Key class?
[quoted text clipped - 23 lines]
>
> // how to get T outside of Key class?
You can’t. But generally, you will have instantiated a Key<Something>
yourself, so you will know what Something is.
> }
>
> private Map<Key, T> map; // how to define this?
You will want to give a generic parameter to Key as well. If you have
nothing better, use Object or even <?>. Using T there will only work if
T is defined in Preference, which it isn’t. You will need Object and
casting, as in Piotr’s example.
> public <T> T get(Key<T> key) {
>
> // what goes here
Whatever you want.
> return map.get(T);
This is impossible. You cannot use types as parameters. Think of it as
get.(Integer). That doesn’t make sense either. The closest to it is
get(Integer.class), since Integer.class is an object of type
Class<Integer>. But get(T.class) will not work, since T cannot be
resolved at compile time.
> }
>
[quoted text clipped - 3 lines]
>
> }
HTH, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html