Scala类型类和don';我不能很好地继承遗产

Scala类型类和don';我不能很好地继承遗产,scala,scala-cats,Scala,Scala Cats,我目前编写了一些实现类型类的类。我用猫来做这个。在此设置中,我有类A[T]和B[T]尝试将SIsFilterable替换为: implicit def sOrSubtypeIsFilterable [T[+_] <: S[_]] = new Filterable[T] { def filter[A](x: T[A]): T[A] = x } implicit def-sorsubsifilterable[T[+\u]类似的问题是SomevsOption。这就是为什么CAT为Xor,V

我目前编写了一些实现类型类的类。我用猫来做这个。在此设置中,我有类
A[T]
B[T]尝试将
SIsFilterable
替换为:

implicit def sOrSubtypeIsFilterable [T[+_] <: S[_]] = new Filterable[T] {
  def filter[A](x: T[A]): T[A] = x
}

implicit def-sorsubsifilterable[T[+\u]类似的问题是
Some
vs
Option
。这就是为什么CAT为
Xor
Validated
提供智能构造函数的原因,所以你不会得到
Validated.validate[Int]
但得到
Validated[Nothing,Int]
。也许可以对
Q
采用类似的策略?@peterneens:不幸的是,由于Q有一些我想向用户公开的附加功能,
过滤器的返回类型应该是
S[a]
,那么这似乎确实解决了问题。我想知道这是否也适用于cats;当添加第二个隐式时,我可能会遇到隐式定义不明确的问题。为什么返回值应该是
S[A]
?您不希望A过滤后的
Q[…]
仍然是
Q[…]
?在我的用例中,
过滤器有点复杂,只能返回一个
S[a]
val x: B[Int] = ???
b.flatMap(_)
val x: B[Int] = ???
(b: A[Int]).flatMap(_)
object StackOverflowTest {
  class S[+T]

  class Q[+T] extends S[T]

  trait Filterable[F[+_]] {
    def filter[A](x: F[A]): F[A]
  }

  implicit class FilterOps[F[+_], A](y: F[A])(implicit ev: Filterable[F]) {
    def filter: F[A] = ev.filter(y)
  }

  implicit object SIsFilterable extends Filterable[S] {
    def filter[A](x: S[A]): S[A] = x
  }

  //def failing = {
  //  val q = new Q[Int]()
  //  val r = q.filter
  //}

  def working = {
    val q = new Q[Int]()
    val r = (q: S[Int]).filter
  }
}
implicit def sOrSubtypeIsFilterable [T[+_] <: S[_]] = new Filterable[T] {
  def filter[A](x: T[A]): T[A] = x
}