Sign in to follow this  

How generic are Java generics?

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For example, is this legal code?
interface FunctionPointer <E> {
  public E theMethod ();
}

class SomeClass implements FunctionPointer <void> {
  public void theMethod () {
    // Do some stuff
  }
}
Failing that, I suppose I could make an empty VoidPointer class that does nothing and contains no code, and then never do anything with objects of that class, but elegant it ain't.

Share this post


Link to post
Share on other sites
Funny, it compiles when E is some class type but doesn't when it is void or a built in type.

But regardless, I suggest you figure out another way to do this. Generics are fairly weak compared to templates/c++. And in java, you shouldn't need to use function pointers in this way.

What are you trying to do exactly?

Share this post


Link to post
Share on other sites
Basically, due to the joy of type erasure, Java generics type parameters need to be derived from Object, which primitives and void are not.

Share this post


Link to post
Share on other sites
As far as I know, the Types that you pass in must be derived from Object. void doesn't meet this specification so that is probably why it doesn't work. I'm not 100% sure if this is always true though.

EDIT: Beaten by a great deal. I spent far too long typing this out :P

Share this post


Link to post
Share on other sites
If you want to use generics with primitives (not including void type) then you can use the series of classes named Integer, Double, Float, Character, etc. These wrap the primitive types into classes and add some rather useful operations also.

Share this post


Link to post
Share on other sites
OK, I guess I could have figured it out myself by just typing in the code and compiling, as I have now done; but I was posting from work where we only use C++. Anyway, thanks to all who answered.

The reason I asked is that I had this great idea for a scripting language, and wanted to make it easily extensible. A mapping from String to function struck me as a fine thing, and I would really prefer not to hardcode a bunch of if statements. Hence FunctionPointer objects that can be given to a HashTable or similar.

I thought of a way to emulate integer (and other) arguments and return types : The base FunctionPointer class could have a bunch of public, static ints, doubles, and so on, which the caller sets before it calls the actual method, and can retrieve values from afterwards. Breaks encapsulation something fierce, but oh well.

Share this post


Link to post
Share on other sites
There already is a Void class, similar to the Integer/Float/etc class types mentioned above.

If you really have to use generics & function pointers, then just use the Void/Integer/etc classes. Just make sure you are using Java >=1.5, otherwise the boxing will drive you nuts.

Share this post


Link to post
Share on other sites

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this