Scala without covariance

Without covariance  (In presence of Invarinace ) 

class Test[T]   

var x:Test[Any] = new Test[Nothing] //error

doom ,what you see 

 Nothing <: Any, but class Test is invariant in type T.

 

Verbose : Nothing is subclass of Any but  Test class is invariant . 

Means we have not define any hierarchy on Test for T

To fix this .Define type hierarchy on Test for T 

Now Using covariant –

you can define covariance on C for T : C[Tsub] is a subclass of C[T]

class Test[+T]

and then  

var x:Test[Any] = new Test[Nothing] // works 

 

 

Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s