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

covariance question

2 replies
mc
Joined: 2011-10-06,
User offline. Last seen 19 weeks 2 days ago.

Hello,

I created this test superclass (allowing covariant types to be
passed):

class SimpleQueue[+A](val list: List[A]) {

def this() = this(List[A]())
def enqueue[B >: A](item: B): SimpleQueue[B] = new SimpleQueue[B]
(list :+ item)
}

This is the concrete implementation which is supposed to use Double as
element type:

class StrangeIntQueue extends SimpleQueue[Double] {

def enqueue[Double](item: Double): SimpleQueue[Double] = {
println(item.toString())
super.enqueue(item) //<<< THIS DOES NOT COMPILE
}
}

I'm getting the following compilation error:
"type mismatch; found : TypeParameterization.this.SimpleQueue[Any]
required: TypeParameterization.this.SimpleQueue[Double]"

I understand the error message - Scala compiler is down casting Double
into the lowest common type.
However I'm not sure why is this happening? Method 'enqueue' is
parametrized by type Double, so B is Double and A is Double.
So there should be no need to down cast? Or is it that the compiler
does not know the actual type yet and is down casting based on
'enqueue' declaration?

I would appreciate an explanation of this problem, and maybe some info
on how to make it work?
Thanks!

mc
Joined: 2011-10-06,
User offline. Last seen 19 weeks 2 days ago.
Re: covariance question

Never mind, I found the problem:

class StrangeIntQueue extends SimpleQueue[Double] {

def enqueue(item: Double) = { //<<< REMOVED [Double]
println(item.toString())
super.enqueue(item)
}
}

I'm not sure what exactly was I breaking, but that was the problem.
Thanks!

On Feb 6, 4:36 pm, mc wrote:
> Hello,
>
> I created this test superclass (allowing covariant types to be
> passed):
>
>   class SimpleQueue[+A](val list: List[A]) {
>
>     def this() = this(List[A]())
>     def enqueue[B >: A](item: B): SimpleQueue[B] = new SimpleQueue[B]
> (list :+ item)
>   }
>
> This is the concrete implementation which is supposed to use Double as
> element type:
>
>   class StrangeIntQueue extends SimpleQueue[Double] {
>
>     def enqueue[Double](item: Double): SimpleQueue[Double] = {
>       println(item.toString())
>       super.enqueue(item) //<<< THIS DOES NOT COMPILE
>     }
>   }
>
> I'm getting the following compilation error:
> "type mismatch; found : TypeParameterization.this.SimpleQueue[Any]
> required: TypeParameterization.this.SimpleQueue[Double]"
>
> I understand the error message - Scala compiler is down casting Double
> into the lowest common type.
> However I'm not sure why is this happening? Method 'enqueue' is
> parametrized by type Double, so B is Double and A is Double.
> So there should be no need to down cast? Or is it that the compiler
> does not know the actual type yet and is down casting based on
> 'enqueue' declaration?
>
> I would appreciate an explanation of this problem, and maybe some info
> on how to make it work?
> Thanks!

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: covariance question
you added a type parameter and named it Double, which shadowed the actual primitive Double

def doh[String](s:String):java.lang.String = s

tools like intellij idea prevent this type of error. this is what they (can) do (if configured to do it):
 


you can immediately spot the problem



Am 06.02.2012 22:44, schrieb mc:
1e1ac6a4-334c-4bbb-93b3-f1fff3348a89 [at] p21g2000yqm [dot] googlegroups [dot] com" type="cite">
Never mind, I found the problem:

  class StrangeIntQueue extends SimpleQueue[Double] {

    def enqueue(item: Double) = { //<<< REMOVED [Double]
      println(item.toString())
      super.enqueue(item)
    }
  }

I'm not sure what exactly was I breaking, but that was the problem.
Thanks!

On Feb 6, 4:36 pm, mc  wrote:
Hello,

I created this test superclass (allowing covariant types to be
passed):

  class SimpleQueue[+A](val list: List[A]) {

    def this() = this(List[A]())
    def enqueue[B >: A](item: B): SimpleQueue[B] = new SimpleQueue[B]
(list :+ item)
  }

This is the concrete implementation which is supposed to use Double as
element type:

  class StrangeIntQueue extends SimpleQueue[Double] {

    def enqueue[Double](item: Double): SimpleQueue[Double] = {
      println(item.toString())
      super.enqueue(item) //<<< THIS DOES NOT COMPILE
    }
  }

I'm getting the following compilation error:
"type mismatch; found : TypeParameterization.this.SimpleQueue[Any]
required: TypeParameterization.this.SimpleQueue[Double]"

I understand the error message - Scala compiler is down casting Double
into the lowest common type.
However I'm not sure why is this happening? Method 'enqueue' is
parametrized by type Double, so B is Double and A is Double.
So there should be no need to down cast? Or is it that the compiler
does not know the actual type yet and is down casting based on
'enqueue' declaration?

I would appreciate an explanation of this problem, and maybe some info
on how to make it work?
Thanks!

    

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