« Return to Thread: parametric types and variance

Re: parametric types and variance

by Daniel Sobral :: Rate this Message:

Reply to Author | View in Thread

I recommend reading the Chapter about variance of Programming in Scala.
 
Anyway, notice that what varies is the type ACCEPTED by classify. In this case, classify[AnyRef] is both a supertype of Aa, thus respecting the lower bound, and of String, so a String can be passed to it.
 
If that is confusing, imagine you defined classify to be:
 
 def classify(t: AnyRef) = t.toString
Can you pass a String to such a method? Of course you can, because a String is a subtype of AnyRef. You can pass pretty much anything, except Java primitive types (Scala's AnyVal). Since AnyRef is a supertype of AA, then this is a valid definition for classify.
On Thu, Jul 9, 2009 at 1:20 PM, Christoph Drießen <ced@...> wrote:
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





--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

 « Return to Thread: parametric types and variance