Reputation: 145
I was trying to write some simple method toList
...
trait Stream[+A] {
def uncons: Option[(A, Stream[A])]
def isEmpty: Boolean = uncons.isEmpty
def toList[A]: List[A] = this.uncons match {
case Some((h,t)) => h::t.toList
case None => List()
}
}
however, this results in the following error:
type mismatch; found : x$1.type (with underlying type A) required: A
I don't understand why this code doesn't work. Probably I am missing something very obvious :(
Upvotes: 1
Views: 228
Reputation: 463
The problem is in your toList
method definition.
By doing:
def toList[A]: List[A] = this.uncons match { ... }
You are actually defining a new type A
in which your method is parameterized.
Just declare the method as:
def toList: List[A] = this.uncons match { ... }
and you are good to go (this definition would be using the same A
defined in the class)
Upvotes: 5