【问题标题】:Import implicits from object that passed as method parameter vs class constructor parameter从作为方法参数与类构造函数参数传递的对象导入隐式
【发布时间】:2016-12-06 05:00:46
【问题描述】:

我有以下代码:

case class Custom(value: Int)
case class Custom2(value: Float)

case class MappedEncoding[I, O](f: I => O)

trait Decoders {
  type BaseDecoder[T] = () => T
  type Decoder[T] <: BaseDecoder[T]
}

trait ConcreteDecoders extends Decoders {
  type Decoder[T] = ConcreteDecoder[T]

  case class ConcreteDecoder[T](decoder: () => T) extends BaseDecoder[T] {
    def apply(): T = decoder()
  }

  implicit def optionDecoder[T](implicit d: Decoder[T]): Decoder[Option[T]] =
    ConcreteDecoder[Option[T]](() => Some(d()))

  implicit def mappedDecoder[I, O](implicit mapped: MappedEncoding[I, O], decoder: Decoder[I]): Decoder[O] =
    ConcreteDecoder[O](() => mapped.f(decoder()))

  implicit def intDecoder: Decoder[Int] = ConcreteDecoder[Int](() => 1)
  implicit def floatDecoder: Decoder[Float] = ConcreteDecoder(() => 1)

}

class ConcreteContext extends ConcreteDecoders {
}

case class TestObject() {

  implicit val customDecoder = MappedEncoding[Int, Custom](Custom)
  implicit val custom2Encoder = MappedEncoding[Custom2, Float](_.value) // 1
  implicit val custom2Decoder = MappedEncoding[Float, Custom2](Custom2)

  def a(c: ConcreteContext): Unit = {
    import c._
    implicitly[Decoder[Option[Custom]]] // 2
//  implicitly[Decoder[Float]]          // 3
    implicitly[Decoder[Option[Float]]]
    ()
  }
}


object Main extends App {
  implicit val c = new ConcreteContext()

  TestObject().a(c)
  // TestObject(a).()
}

它在 Scala 2.11.8 和 2.12.0 中无法编译并出现错误:

diverging implicit expansion for type c.Decoder[Option[Float]]
[error] starting with method intDecoder in trait ConcreteDecoders
[error]     implicitly[Decoder[Option[Float]]]

使用-Xlog-implicits 选项会产生较长的输出,但最有趣的部分是:

floatDecoder is not a valid implicit value for c.Decoder[Float] because:
[info] diverging implicit expansion for type c.Decoder[T]
[info] starting with method intDecoder in trait ConcreteDecoders
[info]     implicitly[Decoder[Option[Float]]]

c: CustomContext 从方法参数移动到案例类构造函数参数使其编译。我认为可能是它改变了隐含的搜索范围。

以下操作之一也使其编译:

  • 1 标记的注释行(== 第 1 行)
  • 评论第 2 行
  • 取消注释第 3 行

看起来解析 implicitly[Decoder[Option[Custom]]] 会使 Scala 编译器处于影响解析 implicitly[Decoder[Option[Float]]] 的状态。

为什么会发生这种情况,如何在不从方法参数中移动 c: ConcreteContext 的情况下使其编译?

P.S. 这是重现问题的简化代码。真正的代码要复杂得多,我需要支持ConcreteContext作为方法参数传递的情况。

【问题讨论】:

  • 这里肯定有问题。这似乎与SI-9625 有点相关。当您将方法参数转换为构造函数参数时,该问题也会消失。在旁注中,让您的 Decoder 成为 Function0 的子类型也可能不是最好的主意。
  • @Jasper-M 由于stackoverflow.com/q/40391732/746347,我将Decoder 设为Function0 的子类型。
  • def a(c: ConcreteContext): Unit 内的明显解决方法def a(c: ConcreteContext): Unit 是否足以满足您的需求?
  • 谢谢!这很有趣,即使val c0 = c; import c0._ 也足够了。但我应该指出我不能改变def a() 的实现。这是我图书馆的用户编写的代码。所以我们应该想想我们可以用ConcreteContextDecoders 实现做些什么。当然,首先我想找到这些奇怪事物的解释。

标签: scala implicit


【解决方案1】:

这不是一个完整的答案。

我没有任何令人满意的解释为什么提到另一个隐式可以将分辨率排除在循环之外,由mappedEncodercustom2Encoder + custom2Decoder 对创建。我只能猜测implicitly[Decoder[Float]] 的存在正在提高floatDecoder 的优先级,而不是注意到的custom2... 循环。

但是有一个很好的解决方案,它可能不是最好但可行的选择,称为 shapeless.Lazy。它有时可以用来代替LowPriority 分解来处理更可怕的情况。

您可以将mappedDecoder 重写为

import shapeless.Lazy

implicit def mappedDecoder[I, O]
  (implicit mapped: MappedEncoding[I, O], 
            decoder: Lazy[Decoder[I]]): Decoder[O] =
    ConcreteDecoder[O](() => mapped.f(decoder.value()))

使原始代码工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2014-07-15
    • 2013-12-12
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    相关资源
    最近更新 更多