I want to just reverse the items in this collection, so I decided to require that it be part of Traversable, and any of the subtypes for it, and have it return a Traversable, but I think I may need to use a variance on that also, but, at the moment I get a compiler error, using Scala 2.10.0-M5.
trait Polynomials {
def coefficients[+A <: Traversable[T]](x:A):Traversable[A] = x.foldLeft(Traversable[A]())((b,a) => a :: b)
}
These are the errors I am getting, and I am not certain what I did wrong.
Description Resource Path Location Type
']' expected but identifier found. Polynomials.scala line 4 Scala Problem
'=' expected but ']' found. Polynomials.scala line 4 Scala Problem
illegal start of simple expression Polynomials.scala line 5 Scala Problem
Scala doesn’t have use-site variance, so
+Ais not allowed in a method, only in a class. It doesn’t really make sense here anyway; I don’t think you’ll miss it. Also, you need aTsomewhere, either on the trait or on the method. And::isn’t defined on traversable, but aListis a traversable, so you can use one. And you’re actually returning aTraversable[T], not aTraversable[A]. So:But the
A <: Traversable[T]isn’t really buying you anything since subtyping gets you that anyway. So shorter and simpler is