导入不会在范围内带来隐含

Import does not bring implicits in scope

我遇到一个关于范围内 implicits 无法访问的错误:

Error:(38, 68) could not find implicit value for parameter strategy: XXX.NeoStrategy[T] (summoner: Summoner, v: String) => summoner.summonEvaluation[T](v)

我执行蒂姆对那个问题的回答:

我尝试在 TypeTable 范围内导入 implicit object 策略:

  import XXX.NeoStrategies._

但没有成功。

以下是我要使用的基本逻辑的各个文件:

  object TypeLib {
    sealed trait Type_top
    trait Type_A extends Type_top
    trait Type_B extends Type_top
  }
  trait NeoStrategy[T <: Type_top] {
    def evaluate(v: String, helper: Helper): Int
  }

  object NeoStrategies {
    implicit object NeoStrategy_A extends NeoStrategy[Type_A] {
      def evaluate(v: String, helper: Helper): Int = 1
    }
    implicit object NeoStrategy_B extends NeoStrategy[Type_B] {
      def evaluate(v: String, helper: Helper): Int = 2
    }
  }
  case class Helper(name: String) {
    def summonEvaluation[T <: Type_top](v: String)(implicit strategy: NeoStrategy[T]): Int = {
      strategy.evaluate(v, this)
    }
  }
  trait TypeOMap {
    protected def computeStuff[T <: Type_top]: (Helper, String) => Int
    protected val computeMap: Map[String, (Helper, String) => Int]
  }
  import XXX.NeoStrategies._

  trait TypeTable extends TypeOMap {
    override protected def computeStuff[T <: Type_top]: (Helper, String) => Int = {
      (helper: Helper, v: String) => helper.summonEvaluation[T](v)
    }
    override protected val computeMap = Map(
      "a" -> computeStuff[Type_A],
      "b" -> computeStuff[Type_B]
    )
  }
class Summoner extends TypeTable {

  def callsMapAndEvaluates(typeIdentifier: String, helper: Helper, param: String): Double = {

    computeMap(typeIdentifier)(helper, param)
  }
}
object StackO {

  def main(args: Array[String]): Unit = {

    val mySummoner = new Summoner

    // mySummoner allows the selecting of a given type with
    // its "typeIdentifier" input in combination with the "TypeTable" it extends
    val r = mySummoner.callsMapAndEvaluates("a", Helper("make it right"), "I, parameter")
  }
}

这不是我第一次使用 implicits 但不是像上面的 computeMap 这样的东西。不过,我明白其中的逻辑,但没能把它弄对。

如何让 summoner.summonEvaluation[T](v) 找到所需的 implicit

只需添加上下文绑定

override protected def computeStuff[T <: Type_top : NeoStrategy] ...

您似乎想使用单例类型。在 Scala 2.12 + Shapeless

  import shapeless.Witness

  object TypeLib {
    sealed trait Type_top
    trait Type_A extends Type_top
    trait Type_B extends Type_top
  }

  import TypeLib._

  trait NeoStrategy[S <: String] {
    type T <: Type_top
    def evaluate(v: S, summoner: Summoner): Int
  }

  object NeoStrategy {
    type Aux[S <: String, T0 <: Type_top] = NeoStrategy[S] { type T = T0 }
    def mkStrategy[S <: String, T0 <: Type_top](f: (S, Summoner) => Int): Aux[S, T0] = new NeoStrategy[S] {
      override type T = T0
      override def evaluate(v: S, summoner: Summoner): Int = f(v, summoner)
    }

    implicit val NeoStrategy_A: NeoStrategy.Aux[Witness.`"a"`.T, Type_A] = mkStrategy((_, _) => 1)
    implicit val NeoStrategy_B: NeoStrategy.Aux[Witness.`"b"`.T, Type_B] = mkStrategy((_, _) => 2)
  }

  case class Summoner(name: String) {
    def summonEvaluation[S <: String](s: Witness.Aux[S])(implicit
      strategy: NeoStrategy[S]): Int = {
      strategy.evaluate(s.value, this)
    }
  }

  def main(args: Array[String]): Unit = {

    val mySummoner = Summoner("stack question")

    val r = mySummoner.summonEvaluation("a")
    val r1 = mySummoner.summonEvaluation("b")

    println(r) // 1
    println(r1) // 2
  }

在 Scala 2.13 中

  object TypeLib {
    sealed trait Type_top
    trait Type_A extends Type_top
    trait Type_B extends Type_top
  }

  import TypeLib._

  trait NeoStrategy[S <: String with Singleton] {
    type T <: Type_top
    def evaluate(v: S, summoner: Summoner): Int
  }

  object NeoStrategy {
    type Aux[S <: String with Singleton, T0 <: Type_top] = NeoStrategy[S] { type T = T0 }
    def mkStrategy[S <: String with Singleton, T0 <: Type_top](f: (S, Summoner) => Int): Aux[S, T0] = new NeoStrategy[S] {
      override type T = T0
      override def evaluate(v: S, summoner: Summoner): Int = f(v, summoner)
    }

    implicit val NeoStrategy_A: NeoStrategy.Aux["a", Type_A] = mkStrategy((_, _) => 1)
    implicit val NeoStrategy_B: NeoStrategy.Aux["b", Type_B] = mkStrategy((_, _) => 2)
  }

  case class Summoner(name: String) {
    def summonEvaluation[S <: String with Singleton](s: S)(implicit
      value: ValueOf[S],
      strategy: NeoStrategy[S]): Int = {
      strategy.evaluate(s, this)
    }
  }

  def main(args: Array[String]): Unit = {

    val mySummoner = Summoner("stack question")

    val r = mySummoner.summonEvaluation("a")
    val r1 = mySummoner.summonEvaluation("b")

    println(r) // 1
    println(r1) // 2
  }

潜在的问题是:

  override protected def computeStuff[T <: Type_top]: (Helper, String) => Int = {
    (helper: Helper, v: String) => helper.summonEvaluation[T](v) // implicit for NeoStrategy[T]...?
  }

由于 summonEvaluation[T] 需要类型为 NeoStrategy[T] 的隐式参数,这意味着您必须在任何 T 的范围内拥有一个 Type_top 的子类。但是,NeoStrategies 只提供了两个实例:一个用于 Type_AType_B。这对编译器来说还不够。可以理解 - 例如,您没有为

提供任何 NeoStrategy
  • Type_top本身
  • Type_AType_B 的子类(创建完全合法)

有两种基本方法可以处理此问题:

延迟隐式解析

根据其他答案,与其尝试解析内部隐式 computeStuff,不如在其中添加一个上下文绑定。如果仅当您知道 T 是什么时才达到必须提供隐式的点,则不必为任何可能的子类型提供实例。

为所有可能的子类型提供隐式

如果你绝对想在 computeStuff 中保留隐式解析,你将不得不提供一个方法

  implicit def getNeoStrategy[T <: Type_top] : NeoStrategy[T] = ???

不幸的是,这样做可能会涉及一系列反射和边缘情况的潜在运行时错误,因此我建议在 computeStuff.

上绑定上下文