简体   繁体   English

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

[英]How can I have a method parameter with type dependent on an implicit parameter?

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]
}

You can see my intent here. 你可以在这里看到我的意图。 I define a type in JsonOps to encapsulate a structure dependant on J. Then later when I want to use this, I have a function that implicitly passes a JsonOps[J] object and also a parameter of type ObjectFields. 我在JsonOps中定义了一个类型来封装依赖于J的结构。然后当我想使用它时,我有一个隐式传递JsonOps [J]对象的函数以及ObjectFields类型的参数。

Problem is, ObjectFields is defined in ops, which occurs after the stuff in the signature. 问题是,ObjectFields是在ops中定义的,它发生在签名中的东西之后。

How can I unscramble this? 我怎么解读这个?

The second def works, but I don't like passing Any around. 第二个def工作,但我不喜欢传递任何周围。 I'd like the compiler to be able to check what's being passed in. 我希望编译器能够检查传入的内容。

You should introduce one more type parameter for compilerLikey and write JsonOps with refinement 您应该为compilerLikey引入另一个类型参数,并使用细化编写JsonOps

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 }) = {}

or using Aux-pattern 或使用Aux-pattern

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]) = {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 另一个隐式方法参数的依赖类型的方法参数 - 是否可能? - Method parameter of dependent type of another implicit method parameter - is it possible? 如何使用包含从属类型的隐式参数组对该方法进行编码? - How do I encode this method with an implicit parameter group which contains a dependent type? 我有一个带有隐式参数的函数。 如何使用某个类实例的隐式参数隐式传递参数? - I have a function which takes an implicit parameter. How can I pass the parameter implicitly using implicit parameters of an instance of some class? 找不到类型下界的方法的参数的隐式转换 - Can not find implicit conversion for a parameter of method with lower type bound Scala如何隐式使抽象方法成为有效参数,以便可以在Java中使用方法 - How scala implicit make abstract method valid parameter so I can use method in java Scala 中隐式值方法的类型参数 - Circe - Type parameter for implicit valued method in Scala - Circe 隐式转换器可以有一个by-name参数吗? - Can an implicit converter have a by-name parameter? 如何将参数传递给存在类型的方法? - How can I pass an parameter to a method of an existential type? 带有隐式参数的调用方法 - Calling method with an implicit parameter 方法参数可以用作隐式转换的隐式参数吗? - Can a method argument serve as an implicit parameter to an implicit conversion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM