I have function with a few type parameters. How to tell compiler that these types are actually in type class?
trait Sizeable[-T, +R] {
def mySize(x: T): R
}
implicit object StringSize extends Sizeable[String, Int] {
def mySize(s: String) = s.length
}
def sum[T, R: Sizeable[T, R]](xs: List[T]): R = xs.map(x => x.mySize)
The error is:
Error:(9, 14) A$A40.this.Sizeable[T,R] does not take type parameters
def sum[T, R: Sizeable[T, R]](xs: List[T]): R = xs.map(x => x.mySize);}
You can't use a context bound in this case. You'll have to write the implicit parameter explicitly:
def foo[T, R](xs: List[T])(implicit sizeable: Sizeable[T,R]): List[R] =
xs.map(x => sizeable.mySize(x))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.