[英]Type Class with many type parameters
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.