
On Thu, 2008-10-16 at 12:27 -0700, Robert Greayer wrote:
--- On Thu, 10/16/08, Jonathan Cast
wrote: So if I say
void wrong(List<?> foo, List<?> bar)
I get two /different/ type variables implicitly filled in?
If I declare a generic class, and then have a method, is there a way, in that method's parameter list, to say `the type parameter that was supplied when my class was instantiated'?
Yes - class Foo<T> { ... void right(List<T> foo, List<T> bar) { foo.add(bar.get(0)); }
Can also do it at the method level...
void <T> alsoRight(List<T> foo, List<T> bar) { ... }
Yikes. So, in this instance, the difference between Haskell and Java is: if you want to disallow that call to wrong, in Haskell you can...
Not exactly... Java disallows 'wrong' from being written (without class casts and such), because it disallows calling a method which has a wildcard type in a contravariant position. IIRC, Scala handles all this more elegantly. If you stay away from '?', and stick to type variables with Java generics, though, how type checking with generics works in Java should be mostly unsurprising to a Haskeller.
Oh, good. Daryoush Mehrtash: It looks like the answer to your original question --- gotchas with Java generics vs. Haskell polymorphism --- is that the `gotchas' you linked to are consequent on using ? in your code, instead of true type variables. So the truly problematic construct is ?, and the difference is just that Haskell doesn't have an analogue to it. jcc