Generally speaking in situations like this, you need to explicitly pass a Class object in (generically parameterised) which is used for the return value.
Up vote 3 down vote favorite share g+ share fb share tw.
We have a function more or less like the following. However we currently return List which in function bla() would return List at runtime. I'm looking for a way to make both List = toubleFuction(foo, bar.getCLass());; and List = toubleFuction(foo, bar.getCLass());; possible.
Basicaly I want it to return List which would be compatible with interface however this gives the following error *Type mismatch: cannot convert from List to List* is there any way to make this return type possible or does runtime erasure make this impossible public List troubleFunction( T1 in, Class clazz) { return in. DoStuffWhichGeneratesAlistOF(clazz) } public void bla() { Foo foo = new Foo(); // implements interface Bar bar = new Bar(); // implements interface List ifaces = toubleFuction(foo, bar.getCLass()); List mustAlsoWork = toubleFuction(foo, bar.getCLass()); } edit: in a lot of the existing code base the method is called like List result = troubleFunction(List list, Bar. Class); thus this return type must stay compatible (rewrite/re-factor is not an option) essentially I want the method to return List if called as troublefunction(foo, Bar.
Class); and List when called as troublefunction(foo, Bar. Class); java generics super return-type link|improve this question edited Jan 28 '11 at 14:22GManNickG79.4k11132275 asked May 26 '09 at 14:48pvgoddijn7421820 50% accept rate.
In Java a function is called a method. – eleven81 May 26 '09 at 14:50 Interface is not a reserved word? – José Leal May 26 '09 at 14:56 iface is supposed to be a List?
– Tom Hawtin - tackline May 26 '09 at 15:00 You mean "and List when called as troublefunction(foo, Foo. Class);", right? – Michael Myers?
Jun 4 '09 at 14:45.
Generally speaking in situations like this, you need to explicitly pass a Class object in (generically parameterised) which is used for the return value. However it looks like you've done this already in your case, so would it not work for troubleFunction to be declared to return List? Alternatively, if you want to keep it general then have it return List.
You're not giving us enough information to really tell what you need to do. For example, you didn't give us the type signature of doStuffWhichGeneratesAlistOF() or tell us what it does. And you didn't tell us what the type of the "in" argument has to do with all of this.
Sure, it's possible to have the return type of a method be generic. For example, public List troubleFunction(Interface in, Class clazz) { List result = new ArrayList(); result. Add(clazz.newInstance()); return result; } And then you could call the method directly like this and it would work (you don't need to specify the type parameter explicitly because it's inferred from the assignment): List iface = this.
TroubleFunction(foo, bar.getCLass()); But seeing as how in your code above you return the result of in. DoStuffWhichGeneratesAlistOF(clazz), you would probably have to make the return type of that method generic also. But I can't really help you on that because we don't have any information on that method.
The in function returns an list of instances of class Bar when called as foo. DoStuffWhichGeneratesAlistOF(Bar. Class) – pvgoddijn Jun 4 '09 at 12:07.
As I understand it, the argument types are looked at before the target type to infer the generic arguments. So, I guess you need to explicitly specify the generic arguments, which I think goes something like this: List iface = this. TroubleFunction(foo, bar.getCLass()); where public List troubleFunction( T in, Class clazz ) {.
I've looked at this again and the problem was that I wanted to use a 'super' return type the signature I was looking for was more or less: public List getAList(Class clazz); which is not possible.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.