My utility functions return
List<? extends Human>
Some of the public functions want to return List of specific types, say a List<User>
What is the cleanest way to downcast the result so we can return List<User> when the worker function returns a
List<? extends Human> which is actually a List< humans who are actually User object>
Rob Prime wrote:But the cast will issue a warning, because the List may be a List<Manager> instead of a List<User>.
Pat, do you need to add objects to your List? If no, then using List<? extends User> is just fine.
Right, the list may actually contain Ranchers, since Ranchers are Human (usually)
Only some functions explicitly return List<Rancher> others return List<User>
Not sure I grok your suggestion, are you saying that the getUsers() function should return List<? extends User>
Rob Prime wrote:How about using a Class as an extra parameter?
It works, but I really dislike using the Class as an extra parameter to fix flaws in the way Generics work, so I do it only when I really, really have to.
Its a minor thing, but shows how they grafted generics onto Java long after the initial design. The curse of backwards compatibility.
I'll just use the cast as someone mentioned upthread.
Thanks to all