- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
covariance question
Mon, 2012-02-06, 22:36
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!
Mon, 2012-02-06, 23:41
#2
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:
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, mcwrote: 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!
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!