我想要做这样的事情:更高版本类型的上下文绑定?
def fold[C[A]](implicit ev: Foldable[A]): A
我越来越not found: type A
我知道,我能做到这一点,而不是:
def fold[C[_], A: Foldable]: A
但是,我宁愿援引作为fold[List[Int]]
比fold[List, Int]
我想要做这样的事情:更高版本类型的上下文绑定?
def fold[C[A]](implicit ev: Foldable[A]): A
我越来越not found: type A
我知道,我能做到这一点,而不是:
def fold[C[_], A: Foldable]: A
但是,我宁愿援引作为fold[List[Int]]
比fold[List, Int]
我打了一下它,并与助手型类想出了:
trait Helper[M[_], CA] {
type C[_]
type A
implicit def ma: M[A]
}
object Helper {
implicit def instance[M0[_], C0[_], A0](implicit ma0: M0[A0]) = new Helper[M0, C0[A0]] {
type C[X] = C0[X]
type A = A0
val ma: M0[A0] = ma0
}
}
我知道的名字是非常通用的,我建议寻找更有意义的名称。
现在不是需要一个隐式类型的Foldable[A]
你需要,而不是一个隐含的Helper[Foldable, CA]
其中CA
是必须在你的例子对List[Int]
匹配类型:
def fold[CA](implicit helper: Helper[Foldable, CA]): helper.A
举个例子:
def fold[CA](implicit helper: Helper[Foldable, CA]): helper.A = {
import helper._
println(implicitly[Foldable[A]])
null.asInstanceOf[A]
}
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class Foldable[A](name: String)
implicit val stringFoldable = Foldable[String]("String")
implicit val intFoldable = Foldable[Int]("Int")
implicit val floatFoldable = Foldable[Float]("Float")
def fold[CA](implicit helper: Helper[Foldable, CA]): helper.A = {
import helper._
println(implicitly[Foldable[A]])
null.asInstanceOf[A]
}
// Exiting paste mode, now interpreting.
defined class Foldable
stringFoldable: Foldable[String] = Foldable(String)
intFoldable: Foldable[Int] = Foldable(Int)
floatFoldable: Foldable[Float] = Foldable(Float)
fold: [CA](implicit helper: Helper[Foldable,CA])helper.A
scala> fold[List[String]]
Foldable(String)
res0: String = null
scala> fold[List[Int]]
Foldable(Int)
res1: Int = 0
scala> fold[List[Float]]
Foldable(Float)
res2: Float = 0.0
下面是我想到的:
trait Foo[T, A]
implicit def makeFoo[A, M[_]] = new Foo[M[A], A] {}
class Helper[T] {
def apply[A]()(implicit ev: Foo[T, A]) = ev
}
def bar[T] = new Helper[T]
bar[List[Int]]()
//Foo[List[Int],Int] = [email protected]
如果你真的想要一个无参数的方法,那么这对空白的对可能并不理想,但我现在看不到如何解决这个问题。
如何折叠有用而没有折叠?当你可以依靠类型推断时,两个类型参数是很好的:'def fold [A:Foldable,C [_]](ca:C [A])=' –
@ m-z:这只是一个人为的例子。用'fooBar'替换fold这个词 – pathikrit
我知道,但通常你会有一个'C [A]'的参数,它可以让你推断出类型参数而不是手动提供它们。换句话说,我相当确定没有办法只用一个类型参数来实现你所要求的。 –