[Haskell-cafe] Re: What I wish someone had told me...

Robert Greayer robgreayer at yahoo.com
Thu Oct 16 15:27:16 EDT 2008

--- On Thu, 10/16/08, Jonathan Cast <jonathanccast at fastmail.fm> 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) {

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.


More information about the Haskell-Cafe mailing list