问题描述
我正在使用 Slick的GetResult
类型类,并希望使用Shapeless派生GetResult[Option[(A, B, C...)]]
我想要的东西:
给出一个隐式的GetResult[Option[A]], GetResult[Option[B]], ...
,
隐式生成GetResult[Option[(A, B, ...)]]
Given an implicit GetResult[Option[A]], GetResult[Option[B]], ...
,
implicitly generate a GetResult[Option[(A, B, ...)]]
我尝试过的事情
trait CanGetOption[T] {
def getOption: GetResult[Option[T]]
}
object CanGetOption {
// convenience implicit resolver
def apply[T](implicit canGetOption: CanGetOption[T]): CanGetOption[T] = canGetOption
// base case: HNil
implicit val getHNilOption: CanGetOption[HNil] = from(GetResult { _ => Some(HNil) })
// recursion case: H :: Tail
implicit def getHConsOption[H, Tail <: HList](
implicit getHeadOption: GetResult[Option[H]],
canGetTailOption: CanGetOption[Tail]
): CanGetOption[H :: Tail] = from(GetResult[Option[H :: Tail]] { r =>
val headOpt = getHeadOption(r)
val tailOpt = canGetTailOption.getOption(r)
for(head <- headOpt; tail <- tailOpt) yield head :: tail
})
// generic case: A, given a A <-> Repr conversion
// I also tried moving this into a "LowPriorityImplicits" thing, just in case
implicit def getGenericOption[A, Repr <: HList](
implicit gen: Generic.Aux[A, Repr],
getReprOpt: CanGetOption[Repr]
): CanGetOption[A] = from(GetResult { r =>
val reprOpt = getReprOpt.getOption(r)
reprOpt.map(gen.from)
})
}
implicit def resolveOptionGetter[T: CanGetOption]: GetResult[Option[T]] =
CanGetOption[T].getOption
问题:
导入以上内容后,搜索隐式时似乎未考虑resolveOptionGetter
:
When I've imported the above, the resolveOptionGetter
doesn't seem to be considered when searching for implicits:
scala> implicitly[GetResult[Option[(Int, Int)]]]
<console>:19: error: could not find implicit value for parameter e: scala.slick.jdbc.GetResult[Option[(Int, Int)]]
implicitly[GetResult[Option[(Int, Int)]]]
^
scala> resolveOptionGetter[(Int, Int)]
res1: scala.slick.jdbc.GetResult[Option[(Int, Int)]] = <function1>
为什么编译器在隐式搜索中找不到resolveOptionGetter
?我该怎么办?
Why can't the compiler find resolveOptionGetter
in the implicit search? What can I do to help it?
推荐答案
问题是slick.jdbc.GetResult
是协变的.如果它是不变的,则可以正确地推断类型并可以解决隐式问题.
The thing is that slick.jdbc.GetResult
is covariant. If it were invariant, types would be inferred correctly and implicits would be resolved.
一种解决方法是使用自定义不变类型别名GetResult
隐藏协变slick.jdbc.GetResult
.删除导入slick.jdbc.GetResult
并写入您的源文件
A workaround is hiding covariant slick.jdbc.GetResult
with custom invariant type alias GetResult
. Remove import slick.jdbc.GetResult
and write in your source file
type GetResult[T] = slick.jdbc.GetResult[T]
object GetResult {
def apply[T](implicit f: PositionedResult => T): GetResult[T] = slick.jdbc.GetResult.apply
}
现在implicitly[GetResult[Option[(Int, Int)]]]
进行编译.在Scala 2.12.7 + Shapeless 2.3.3 + Slick 3.2.3中进行了测试.
Now implicitly[GetResult[Option[(Int, Int)]]]
compiles. Tested in Scala 2.12.7 + Shapeless 2.3.3 + Slick 3.2.3.
差异通常会给隐式解决带来麻烦:
Variance often makes troubles for implicit resolution:
https://github.com/scala/bug/issues/10099
https://github.com/locationtech/geotrellis/issues/1292
这篇关于定义第三方类型类的实例,找不到隐式但显式工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!