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.
How generic are Java generics?
For example, is this legal code?
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?
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?
Basically, due to the joy of type erasure, Java generics type parameters need to be derived from Object, which primitives and void are not.
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
EDIT: Beaten by a great deal. I spent far too long typing this out :P
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.
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement