« Return to Thread: parametric types and variance

Re: parametric types and variance

by Christoph Driessen :: Rate this Message:

Reply to Author | View in Thread

Hi Miles,
page 396 of Programming in Scala says: " U >: T defines T as the lower  
bound for U". So U must be a supertype of T. In that way you're right  
and that's how I understand it. But how is it possible to pass in a  
String? String isn't a supertype of Aa.

The change you've suggested does not compile:
covariant type T occurs in contravariant position in type >:  
scala.this.Nothing <: T of type U&0
def classify[U <: T](t: U): String

Cheers,
Christoph



Am 09.07.2009 um 16:31 schrieb Miles Sabin:

> 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