« Return to Thread: parametric types and variance

Re: parametric types and variance

by Miles Sabin :: Rate this Message:

Reply to Author | View in Thread

On Thu, Jul 9, 2009 at 3:23 PM, Christoph Drießen<ced@...> wrote:
> can anyone explain to me why the following code compiles? As I understand
> it, Aa acts as the lower bound in the implementation of classify in class
> AaClassifier and so I wonder why I'm able to pass in a String.

You've got your bounds the wrong way around: that's an upper bound, so
T can be instantiated to the supertypes of Aa which are Any, AnyRef
etc.

What I think you want is,

trait Classifier[+T] {
   def classify[U <: T](t: U): String
}

class AaClassifier extends Classifier[Aa] {
   def classify[T <: Aa](t: T) = t.toString
}

Cheers,


Miles

--
Miles Sabin
tel: +44 (0)7813 944 528
skype:  milessabin
http://www.chuusai.com/
http://twitter.com/milessabin

 « Return to Thread: parametric types and variance