类型级编程Scala中的问题

时间:2015-08-07 03:43:15

标签: scala functional-programming

我正在尝试在scala中执行此操作

abstract class Rel[A: NeoNode, B: NeoNode] {
  val from: A
  val to: B
}
abstract class NeoRel[C <: Rel[A, B] : Mapper, A: NeoNode, B: NeoNode]{
def save(c: C)
}
class NeoRelOperations[C <: Rel[A, B] : ({type L[x <: Rel[A, B]] = NeoRel[x, A, B]})#L : Mapper, A: NeoNode, B: NeoNode](c: C) {
  val neoRel = implicitly[NeoRel[C, B, A]]
def save() = neoRel.save(c)
}
object NeoRelOperations {
  def apply[C <: Rel[A, B] : ({type L[x <: Rel[A, B]] = NeoRel[x, A, B]})#L : Mapper, A: NeoNode, B: NeoNode](c: C) = new NeoRelOperations(c)
}

但我隐含地遇到了问题:

val neoRel = implicitly[NeoRel[C, B, A]]

我在编译器上得到了这个

Error:(88, 26) could not find implicit value for parameter e: com.kreattiewe.neo4s.orm.NeoRel[C,B,A]
val neoRel = implicitly[NeoRel[C, B, A]]
                     ^
Error:(88, 26) not enough arguments for method implicitly: (implicit e: com.kreattiewe.neo4s.orm.NeoRel[C,B,A])com.kreattiewe.neo4s.orm.NeoRel[C,B,A].
Unspecified value parameter e.
  val neoRel = implicitly[NeoRel[C, B, A]]
                         ^
Error:(99, 119) inferred type arguments [C,Nothing,Nothing] do not conform to class NeoRelOperations's type parameter bounds [C <: com.kreattiewe.neo4s.orm.Rel[A,B],A,B]
  def apply[C <: Rel[A, B] : ({type L[x <: Rel[A, B]] = NeoRel[x, A, B]})#L : Mapper, A: NeoNode, B: NeoNode](c: C) = new NeoRelOperations(c)
                                                                                                                      ^
Error:(99, 140) type mismatch;
 found   : C(in method apply)
 required: C(in class NeoRelOperations)
  def apply[C <: Rel[A, B] : ({type L[x <: Rel[A, B]] = NeoRel[x, A, B]})#L : Mapper, A: NeoNode, B: NeoNode](c: C) = new NeoRelOperations(c)
                                                                                                                                           ^
Error:(99, 119) could not find implicit value for evidence parameter of type com.kreattiewe.neo4s.orm.NeoRel[C,A,B]
  def apply[C <: Rel[A, B] : ({type L[x <: Rel[A, B]] = NeoRel[x, A, B]})#L : Mapper, A: NeoNode, B: NeoNode](c: C) = new NeoRelOperations(c)

可能存在上下文绑定处理的另一种解决方案

1 个答案:

答案 0 :(得分:0)

如果您使用implicitly[X],则应该在范围内implicit val x: X = ...implicit def makeX(...): X = ...

你在范围内有NeoRel[C, B, A]隐式val / def吗?