[英]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.