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

时间:2019-06-26 14:26:42

标签: scala implicit

我遇到有关范围内无法访问的implicit的错误:

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

我实现了蒂姆对这个问题的回答:https://stackoverflow.com/a/56668734/3896166

我尝试使用以下命令在implicit object范围内导入TypeTable策略:

  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")
  }
}

这不是我第一次使用implicit,但是没有使用上面的computeMap之类的东西。我仍然了解它的逻辑,但未能做到正确。

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

2 个答案:

答案 0 :(得分:4)

只需添加上下文绑定

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
  }

答案 1 :(得分:1)

潜在问题是这样的:

  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。这对于编译器来说还不够。可以理解,例如-您没有为{p>提供任何NeoStrategy

  • Type_top本身
  • Type_AType_B
  • 子类(完全合法创建)

有两种基本的处理方法:

延迟隐式分辨率

根据其他答案,与其尝试解决computeStuff内部的隐式问题,还不如在其中添加绑定的上下文。如果仅在知道T是什么的情况下才需要提供隐式变量,那么您将不必为任何可能的子类型提供实例。

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

如果绝对要在computeStuff中保留隐式分辨率,则必须提供一种方法

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

不幸的是,对于边缘情况,这样做可能会涉及很多反射和潜在的运行时错误,因此,我建议将上下文绑定在computeStuff上。