在隐式搜索期间如何自动删除Scala特征?

问题描述 投票:3回答:1

我正在使用类型类,并且有些问题会自动导出它们,这些类型无关地扩展了额外的(标记/指标)特征。这很难解释,但这个最小的例子应该清楚我的意思:

// Base type we are working on
trait Food {}

// Marker trait - unrelated to Food's edibility
trait Plentiful {}

// Indicator type class we want to derive
trait IsHarmfulToEat[F<:Food] {}

object IsHarmfulToEat {
  // Rule that says that if some food is harmful to eat, 
  // an enormous amount is so as well
  implicit def ignoreSupply[F1<:Food,F2<:F1 with Plentiful]
                  (implicit isHarmful: IsHarmfulToEat[F1],
                   constraint: F2=:=F1 with Plentiful): IsHarmfulToEat[F2] = 
                         new IsHarmfulToEat[F2]{}
}

// Example of food
case class Cake() extends Food {}

object Cake {
  // Mark Cake as being bad for you
  implicit val isBad: IsHarmfulToEat[Cake] = new IsHarmfulToEat[Cake] {}
}

object FoodTest extends App {
  // Our main program
  val ignoreSupplyDoesWork: IsHarmfulToEat[Cake with Plentiful] = 
    IsHarmfulToEat.ignoreSupply[Cake,Cake with Plentiful] // compiles fine

  val badCake = implicitly[IsHarmfulToEat[Cake]] // compiles fine
  val manyBadCakes = implicitly[IsHarmfulToEat[Cake with Plentiful]] 
  // ^^^ does not compile - I do not understand why
}

(如果我使Plentiful通用和/或添加自我类型的Food,我会得到相同的行为。)

从编译中调查隐式日志,我发现:

Food.scala:33: util.this.IsHarmfulToEat.ignoreSupply is not a valid implicit value for IsHarmfulToEat[F1] because:
hasMatchingSymbol reported error: diverging implicit expansion for type IsHarmfulToEat[F1]
starting with method ignoreSupply in object IsHarmfulToEat
  val manyBadCakes = implicitly[IsHarmfulToEat[Cake with Plentiful]]
                               ^
Food.scala:33: util.this.IsHarmfulToEat.ignoreSupply is not a valid implicit value for IsHarmfulToEat[Cake with Plentiful] because:
hasMatchingSymbol reported error: diverging implicit expansion for type IsHarmfulToEat[F1]
starting with method ignoreSupply in object IsHarmfulToEat
  val manyBadCakes = implicitly[IsHarmfulToEat[Cake with Plentiful]]
                               ^
Food.scala:33: diverging implicit expansion for type IsHarmfulToEat[Cake with Plentiful]
starting with method ignoreSupply in object IsHarmfulToEat
  val manyBadCakes = implicitly[IsHarmfulToEat[Cake with Plentiful]]

在我看来,F1的类型推断正在打破,因为当编译器正在寻找ignoreSupply时,IsHarmfulToEat[Cake with Plentiful]根本没有尝试使用正确的类型。任何人都可以向我解释为什么会这样吗?和/或如何引导编译器尝试正确的类型?和/或以另一种方式实现ignoreSupply规则?

scala type-inference implicit
1个回答
3
投票

如果你使IsHarmfulToEat逆变,下面的代码编译

  trait Food

  trait Plentiful

  trait IsHarmfulToEat[-F <: Food]

  object IsHarmfulToEat {
    implicit def ignoreSupply[F <: Food]
    (implicit isHarmful: IsHarmfulToEat[F]
    ): IsHarmfulToEat[F with Plentiful] =
      new IsHarmfulToEat[F with Plentiful]{}
  }

  case class Cake() extends Food {}

  object Cake {
    implicit val isBad: IsHarmfulToEat[Cake] = new IsHarmfulToEat[Cake] {}
  }

  object FoodTest extends App {
    val ignoreSupplyDoesWork: IsHarmfulToEat[Cake with Plentiful] =
      IsHarmfulToEat.ignoreSupply[Cake]

    val badCake = implicitly[IsHarmfulToEat[Cake]]
    val manyBadCakes = implicitly[IsHarmfulToEat[Cake with Plentiful]]
  }
© www.soinside.com 2019 - 2024. All rights reserved.