• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Contravariant Types in Scala

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After reading a bit about variance in general and how it could be used in Scala, I'm a bit concerned on where would I use a Contravariant type? So by the definition, a Contravariant type in Scala could be defined as below:



I instantiate this ContraVariant by specifying a concrete type as below:



Why would this not work?



What would that mean when assigning a more broader type to a more specific type? That seems to be exactly the reverse of a Co-Variant, but I'm wondering in which situations would this be useful to use? Any thoughts?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Harry wrote:Why would this not work?


Because AnyVal is not a supertype of String.

AnyVal is the supertype for primitive types in Scala. String is not a primitive type, it's a reference type - it's a subtype of AnyRef, but not of AnyVal.
 
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most commonly contravariant parameters are used in functions and function-like structures (callbacks, visitors, etc...). Look at the documentation of Function1 and Function2. Parameters to these functions are contra-variant. In very basic terms it means that "if function accepts object, then it can accept string (or any other reference)". So you can write


This works quite well with the following rules:
* If you are going to return value of T, T can be covariant. If your method (or methods in class) returns an instance of some class, we can use that method or class where users expect an object of superclass to be returned.
* If you are going to receive an argument of type T, T can be contravariant. If your method (or methods in a class) can accept objects of some class, it can accept an object of subclass.
* If you are going to accept and return values of T, then T can't be neither covariant nor contravariant. Mutable collections like ArrayBuffer are example of this rule.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic