简体   繁体   English

尝试使用隐式 class 扩展它时,代表扩展方法不可用?

[英]Rep extension methods unavailable when trying to extend it with implicit class?

I'm trying to extend a Slick Rep with a new method =?= supplied as an extension method via Scala's implicit class :我正在尝试使用新方法=?=通过Scala的implicit class作为扩展方法提供来扩展 Slick Rep

import slick.ast.TypedType
import slick.lifted.LiteralColumn
import slick.lifted.Rep

object RepExtensions {
  implicit class RichRep[T](rep: Rep[T]) {
    def =?=(rhs: Option[T])(implicit tt: TypedType[Boolean]): Rep[Boolean] =
      rhs.map(rep === _).getOrElse(LiteralColumn[Boolean](true))
  }
}

However in this case, for reasons I'm unable to identify, the method === is not visible to the compiler:但是在这种情况下,由于我无法识别的原因,方法===对编译器不可见:

[error] /Users/.../RepExtensions.scala:10:19: value === is not a member of slick.lifted.Rep[T]
[error]       rhs.map(rep === _).getOrElse(LiteralColumn[Boolean](true))
[error]

The === method is defined in Slick's ColumnExtensionMethods but I can't find any documentation explaining how to explicitly import that into scope - it certainly doesn't help to directly import that class itself. ===方法在 Slick 的ColumnExtensionMethods中定义,但我找不到任何文档解释如何将其显式导入 scope - 直接导入 class 本身当然无济于事。 Indeed in the rest of my code where I'm dealing with Rep types, that === method seems to be freely available whenever I need it, without any special effort or explicit importing.实际上,在我处理Rep类型的代码的 rest 中, ===方法似乎在我需要时可以免费使用,无需任何特殊努力或显式导入。

How can I get the === method into scope in this case?在这种情况下,如何将===方法放入 scope 中?

The extension methods are implemented for types via the implicit defs in ExtensionMethodConversions (see the bottom of ExtensionMethods.scala ).扩展方法是通过ExtensionMethodConversions 底部)。 That means, for === to be available, the implicit needs to apply to your type, T .这意味着, ===可用,隐式需要应用于您的类型T As T is unconstrained, it doesn't match any of the implicit defs to bring === into scope.由于T不受约束,它不匹配任何将===带入 scope 的隐式定义。

One way to resolve that is to constrain T to match.解决该问题的一种方法是将T约束为匹配。 For example:例如:

import slick.ast.BaseTypedType
import slick.jdbc.H2Profile.api._ // or whichever database you use

object RepExtensions {
  implicit class RichRep[T : BaseTypedType](rep: Rep[T]) {
    def =?=(rhs: Option[T]): Rep[Boolean] =
      rhs.map(rep === _).getOrElse(LiteralColumn[Boolean](true))
  }
}

The next step up from that is to require a Shape , as described: https://stackoverflow.com/a/43014901/154248 -- but it doesn't look like you need that for this definition.下一步是需要一个Shape ,如下所述: https://stackoverflow.com/a/43014901/154248 - 但看起来你不需要这个定义。

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

相关问题 类型细化的隐式扩展方法 - implicit extension methods on type refinements 尝试将通用 class 传递给我的 csvloader 时,在隐式分辨率中苦苦挣扎 - Struggling with implicit resolution when trying to pass a generic class to my csvloader 如何在Scala中使用隐式参数扩展类 - How to extend class with implicit parameter in Scala 在通过工厂方法与伴随对象扩展类时,如何“扩展”工厂方法 - How to “extend” factory methods when extending class with companion object with factory methods 通过隐式扩展丰富案例类 - Enrich case class via implicit extension 使用隐式类型类(方差+多态)时,Scala无法访问getter方法 - Scala cannot access getter methods when using implicit type-class (variance + polymorphism) 尝试对有序类的实例进行排序时,为什么会出现“发散的隐式展开”错误? - Why am I getting a “ diverging implicit expansion” error when trying to sort instances of an ordered class? 尝试实现“Absurd”类型类时出现隐式错误 - Implicit error when trying to implement the `Absurd` typeclass 当假定不是等效的隐式类不存在时,应用隐式转换 - Implicit conversion is applied when supposedly equivalent implicit class is not 我什么时候应该使用隐式类? - When should I use an implicit class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM