This page is no longer maintained — Please continue to the home page at www.scala-lang.org

non-variance and covariance

2 replies
Kurdy
Joined: 2012-01-05,
User offline. Last seen 42 years 45 weeks ago.

Hi,

In my learning of Scala and after reading some posts, blogs and book
chapters. I could not find a satisfactory manner to solve the problem
below. If someone can point me a way it would be great.

I create a Property class as non-variant see below

class Property[T](val key: String, var value: T) {

def apply(newValue: T) : Unit = value = newValue

def update(newValue: T) : Unit = value = newValue
}

The class is non-variant (because of T and not +T) and mutable
(because of var value : T)

The code below will not compile ( its what I want)

var p1 = new Property[Int]("key.p1", 1)
var p2 = new Property[Long]("key.p2", 2L)
p2 = p1 // Not possible -> OK

Then I create a class Properties like:

class Properties {
private var properties = Set.empty[Property[Any]]

def addProperty(prop: Property[Any]): Unit = properties += prop

def size : Int = properties.size
}

I could use my class Properties only with Property[Any] or by
transforming my class Property to covariant

If I change my class to covariant I change its characteristics

1. The class became Immutable
2. I can assign p1 to p2 (see above)

I couldn't find a way to have either a Property class non-variant and
a class Properties with method add that accept any type of properties
into a single instance.

How could I modify my class Properties to be able to store any type of
Property without changing characteristics of my class property.

Thanks for any advices.

Willy

Brian Maso
Joined: 2011-07-21,
User offline. Last seen 42 years 45 weeks ago.
Re: non-variance and covariance
You can use wildcard parameters to make an invariant parameter effectively covariant. (Wildcards are actually syntactic sugar for existential types -- but the term "existential type" makes it sound so much more complicated!)

scala> class Properties {
      |   private var props = Set.empty[Property[_ <: Any]]
      |   def addProperty(prop: Property[_ <: Any]): Unit = props += prop
      |   def size = props.size
      | }
defined class Properties

And now its no problem using different Property bindings in the same Properties object:

scala> val pi = new Property("int", 1)
pi: Property[Int] = Property@4e3a2053

scala> val ps = new Property("string", "Hello, World!")
ps: Property[java.lang.String] = Property@2533b5db

scala> val props = new Properties
props: Properties = Properties@4bfa79c8

scala> props.addProperty(pi)

scala> props.addProperty(ps)

scala> props.size
res3: Int = 2

Brian Maso


On Wed, Jan 4, 2012 at 4:16 PM, Kurdy <flyingwilly [at] gmail [dot] com> wrote:
Hi,

In my learning of Scala and after reading some posts, blogs and book
chapters.  I could not find a satisfactory manner to solve the problem
below. If someone can point me a way it would be great.

I create a Property class as non-variant see below

class Property[T](val key: String, var value: T) {

 def apply(newValue: T) : Unit = value = newValue

 def update(newValue: T) : Unit = value = newValue
}

The class is non-variant (because of T and not +T) and mutable
(because of var value : T)

The code below will not compile ( its what I want)

var p1 = new Property[Int]("key.p1", 1)
var p2 = new Property[Long]("key.p2", 2L)
p2 = p1 // Not possible -> OK

Then I create a class Properties like:

class Properties {
 private var properties = Set.empty[Property[Any]]

 def addProperty(prop: Property[Any]): Unit = properties += prop

 def size : Int = properties.size
}


I could use my class Properties only with Property[Any] or by
transforming my class Property to covariant

If I change my class to covariant I change its characteristics

1. The class became Immutable
2. I can assign p1 to p2 (see above)

I couldn't find a way to have either a Property class non-variant and
a class Properties with method add that accept any type of properties
into a single instance.

How could I modify my class Properties to be able to store any type of
Property without changing characteristics of my class property.


Thanks for any advices.

Willy



Kurdy
Joined: 2012-01-05,
User offline. Last seen 42 years 45 weeks ago.
Re: non-variance and covariance

Thanks Brian , "existential types" was words I needed Thanks again.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland