Scala 如何使类型依赖于隐式参数的方法参数?

Scala 如何使类型依赖于隐式参数的方法参数?,scala,dependent-type,Scala,Dependent Type,你可以在这里看到我的意图。我在JsonOps中定义了一个类型来封装依赖于J的结构。然后,当我想使用它时,我有一个函数隐式地传递一个JsonOps[J]对象和一个ObjectFields类型的参数 问题是,ObjectFields是在ops中定义的,它发生在签名中的内容之后 我如何解读这一点 第二个def有效,但我不喜欢传递任何信息。我希望编译器能够检查传入的内容。您应该为compilerLikey引入更多的类型参数,并通过细化编写JsonOps trait JsonOps[J] { type

你可以在这里看到我的意图。我在JsonOps中定义了一个类型来封装依赖于J的结构。然后,当我想使用它时,我有一个函数隐式地传递一个JsonOps[J]对象和一个ObjectFields类型的参数

问题是,ObjectFields是在ops中定义的,它发生在签名中的内容之后

我如何解读这一点


第二个def有效,但我不喜欢传递任何信息。我希望编译器能够检查传入的内容。

您应该为
compilerLikey
引入更多的类型参数,并通过细化编写
JsonOps

trait JsonOps[J] {
  type ObjectFields
  def partitionObjectFields(fields: ObjectFields, fieldNames: List[String]): (ObjectFields, ObjectFields)
}

def compilerNoLikey[J](stuff: ops.ObjectFields)(implicit ops:JsonOps[J]) = {}

def compilerLikey[J](stuff: Any)(implicit ops:JsonOps[J]) = {
    val stuff2 = stuff.asInstanceOf[ops.ObjectFields]
}
或使用

trait JsonOps[J] {
  type ObjectFields
  def partitionObjectFields(fields: ObjectFields, fieldNames: List[String]): (ObjectFields, ObjectFields)
}

def compilerLikey[J, OF](stuff: OF)(implicit ops: JsonOps[J] { type ObjectFields = OF }) = {}
trait JsonOps[J] {
  type ObjectFields
  def partitionObjectFields(fields: ObjectFields, fieldNames: List[String]): (ObjectFields, ObjectFields)
}

object JsonOps {
  type Aux[J, OF] = JsonOps[J] { type ObjectFields = OF }
}

def compilerLikey[J, OF](stuff: OF)(implicit ops: JsonOps.Aux[J, OF]) = {}