I am reading Hands-on Scala Programming and I am stuck on recursive typeclass inference.
The section explains how to write a parser from multiple data types. It gives the example below
trait StrParser[T]{ def parse(s: String): T }
object StrParser{
implicit object ParseInt extends StrParser[Int]{
def parse(s: String) = s.toInt
}
implicit object ParseBoolean extends StrParser[Boolean]{
def parse(s: String) = s.toBoolean
}
implicit object ParseDouble extends StrParser[Double]{
def parse(s: String) = s.toDouble
}
}
The above code works well. After that, it moves on to recursive typeclass inference. It shows an implication function below.
implicit def ParseSeq[Int](implicit p: StrParser[Int]) = new StrParser[Seq[Int]]{
def parse(s: String) = s.split(',').toSeq.map(p.parse)
}
However, I face an error after I ran this code. I tried it on Ammonite, Scala, and compile it but everything does not work.
result type of implicit definition needs to be given explicit
I look up the error on google and only found two non-related results. How can I fix this issue? What am I missing?
Int, that would be confusing, use a single letter likeAorT- Second, the error is pretty clear, you have to be explicit with the return type of yourimplicit deftry:implicit def ParseSeq[T](implicit ev: StrParser[T]): StrParser[Seq[T]] =