
So does this mean that the reason for complexity of generics is the Java
inheritance?
BTW, in addition to the article I posted, This site:
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html has a FAQ
on Java generics that is 500+ pages long! In Haskell you have
parametrized types but I don't think you need 500+ page faq to explain it.
daryoush
On Thu, Oct 16, 2008 at 12:27 PM, Jonathan Cast
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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe