- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
covariance question
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!










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!
Re: Re: covariance question
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: