【问题标题】:Implicit view not working - is my implicit def to blame?隐式视图不起作用 - 是我隐式定义的责任吗?
【发布时间】:2021-01-05 21:58:39
【问题描述】:

我在使用隐式视图时遇到了一些问题。我怀疑这是非常微不足道的,可能有一些令人尴尬的简单答案。我有这样的情况,以及(显然,不成功)尝试调试它:

abstract class StoresNumeric[A, T: Numeric] {
  def getNum(self: A): T
}
object StoresNumericSyntax {
  implicit class StoresNumericOps[A, T: Numeric](value: A) {
    def getNum(implicit sn: StoresNumeric[A, T]): T = sn.getNum(value)
  }
}
case class ANumber[T: Numeric](
  num: T
)
implicit def aNumberStoresNumeric[T: Numeric] = 
  new StoresNumeric[ANumber[T], T] {
    def getNum(self: ANumber[T]): T = self.num
  }
val a = ANumber[Int](3)
// 1. Works fine, so explicit conversion possible
aNumberStoresNumeric[Int].getNum(a) 
// 2. Works fine, so implicit conversion possible
implicitly[StoresNumeric[ANumber[Int], Int]].getNum(a) 
// 3. Doesn't work, so implicit conversion not working
println(implicitly[ANumber[Int] => StoresNumeric[ANumber[Int], Int]]) // no implicit view available...
// 4. The holy grail. Doesn't work, for the same reason as above, plus possibly other
a.getNum

我认为这里的问题是我的implicit def,或者更准确地说,我对implicit def 缺乏了解实际上是为了工作。我不确定泛型扮演什么角色 - 第一个泛型参数应该用于表示我实际想要转换的类型,还是可以这样?

感谢您的帮助。

【问题讨论】:

  • 最好改用typeclass - 另外,将您的操作类设为value class 以避免分配。
  • @LuisMiguelMejíaSuárez - 谢谢 - 如果我理解正确,您的意思是从特征而不是抽象类创建类型类?如果是这样,这是一个简化的示例,但我认为它确实需要是一个抽象类,以便能够使用上下文边界来约束泛型参数。一个更详细的类似例子在这里:stackoverflow.com/questions/63769737/…
  • 我的意思是使用 typeclass 而不是 implicit conversion。
  • @LuisMiguelMejíaSuárez - 很抱歉,但我不清楚你的意思 - 隐式转换是作为创建 StoresNumeric 类型类实例的一种手段 - 你的意思是使用另一个类型类吗?我刚刚尝试切换到一个值类,但乍一看这似乎只适用于具体类型类,即那些没有额外泛型参数的类型。
  • 对不起,我误解了你的问题。您已经在使用 typeclass。 - 你只需要在调用 a.getNum 之前导入语法 import StoresNumericSyntax._ - 另外,尝试像这样重新编写你的语法:implicit class StoresNumericOps[A](private val value: A) extends AnyVal {,然后是这样的方法:def getNum[T](implicit sn: StoresNumeric[A, T])

标签: scala generics implicit-conversion typeclass implicit


【解决方案1】:

首先(#3),

implicitly[ANumber[Int] => StoresNumeric[ANumber[Int], Int]]

错了。您不定义从数据类型到类型类的隐式转换,而是定义从数据类型到引入扩展方法的隐式类的隐式转换。所以应该是

implicitly[ANumber[Int] => StoresNumericSyntax.StoresNumericOps[ANumber[Int], Int]]

然后编译。

做好准备,implicitly[A => B] 并不总是检查是否存在从 A 到 B 的隐式转换。 *(见下文)

其次(#4),当你使用扩展方法(a.getNum)时,你应该导入语法对象:

import StoresNumericSyntax._
(a: StoresNumericOps[ANumber[Int], Int]).getNum

编译时

import StoresNumericSyntax._
a.getNum

产生(scalacOptions += "-Xlog-implicits" 开启)

 Warning:
StoresNumericOps is not a valid implicit value for App.a.type => ?{def getNum: ?} because:
ambiguous implicit values:
 both object BigIntIsIntegral in object Numeric of type scala.math.Numeric.BigIntIsIntegral.type
 and object IntIsIntegral in object Numeric of type scala.math.Numeric.IntIsIntegral.type
 match expected type Numeric[T]

如果您导入 IntIsIntegral,您会将此隐式添加到本地范围(在此之前它仅在隐式范围内),因此您将使其“优先级”“高于”BigIntIsIntegral 之一.试试

import StoresNumericSyntax._
import Numeric.IntIsIntegral
a.getNum

它编译。

Scala 2.13.3.

另请参阅如何调试隐式(在编译时):In scala 2 or 3, is it possible to debug implicit resolution process in runtime?


* 例如,如果您将隐式类修改为 @LuisMiguelMejíaSuárez 在 cmets 中建议

object StoresNumericSyntax {
  implicit class StoresNumericOps[A](private val value: A) extends AnyVal {
    def getNum[T: Numeric](implicit sn: StoresNumeric[A, T]): T = sn.getNum(value)
  }
}

然后

import StoresNumericSyntax._
implicitly[ANumber[Int] => StoresNumericOps[ANumber[Int]]]

无法编译

 Warning:
StoresNumericOps is not a valid implicit value for ANumber[Int] => StoresNumericSyntax.StoresNumericOps[ANumber[Int]] because:
hasMatchingSymbol reported error: type mismatch;
 found   : StoresNumericSyntax.StoresNumericOps.type
 required: ANumber[Int] => StoresNumericSyntax.StoresNumericOps[App393.ANumber[Int]]

手动解决时

implicitly[ANumber[Int] => StoresNumericOps[ANumber[Int]]](new StoresNumericOps(_)) 

编译和

import StoresNumericSyntax._
a: StoresNumericOps[ANumber[Int]]

也可以编译。

但是如果我删除extends AnyVal

object StoresNumericSyntax {
  implicit class StoresNumericOps[A](private val value: A) /*extends AnyVal*/ {
    def getNum[T: Numeric](implicit sn: StoresNumeric[A, T]): T = sn.getNum(value)
  }
}

然后

import StoresNumericSyntax._
implicitly[ANumber[Int] => StoresNumericOps[ANumber[Int]]]

编译。

另外,如果我将隐式类拆分为一个类 + 一个隐式转换

object StoresNumericSyntax {
  /*implicit*/ class StoresNumericOps[A](private val value: A) extends AnyVal {
    def getNum[T: Numeric](implicit sn: StoresNumeric[A, T]): T = sn.getNum(value)
  }

  implicit def toStoresNumericOps[A](value: A): StoresNumericOps[A] =
    new StoresNumericOps(value)
}

然后

import StoresNumericSyntax._
implicitly[ANumber[Int] => StoresNumericOps[ANumber[Int]]]

编译。

为什么implicitly[A => B]和val x: B = ??? : A不一样在这里解释一下:

In scala, are there any condition where implicit view won't be able to propagate to other implicit function?

When calling a scala function with compile-time macro, how to failover smoothly when it causes compilation errors?

Scala Kleisli throws an error in IntelliJ

What are the hidden rules regarding the type inference in resolution of implicit conversions?

Scala: `ambigious implicit values` but the right value is not event found

extends AnyVal 的存在/不存在对隐式解析的影响可能是一个错误,但隐式实例 (implicitly[A => B]) 和隐式转换 (val x: B = ??? : A) 之间的差异或多或少是有意的(不同的类型推断策略,使用解析类型参数)。

【讨论】:

  • 您好 Dmytro,一如既往地感谢您提供这些出色的答案。昨天当我说这一切都已修复时,我有点仓促 - 使用值类修复了我的真实示例,而不是上面的简化示例。我正在使用 2.13.2,我得到的结果与您相同 - 导入 IntIsIntegral 使 a.getNum 工作,并为隐式视图使用非值类并导入 StoresNumericSyntax 使 implicitly[A => B] 行工作。听起来您不太喜欢 Luis 提到的 extends Anyval 方法?
  • 我有点惊讶,正是隐含的含糊不清导致a.getNum 无法工作——您可能认为编译器可以显示比value getNum is not a member of 更有用的错误消息......因为它可能会告诉您存在与隐式发生的冲突。
  • @Chrisper "编译器可以显示更有用的错误消息" 但是,如果您打开 -Xlog-implicits,它确实会提供有用的消息作为警告。 “听起来你不太喜欢extends Anyval” 值类还可以,这是一种避免运行时开销的优化,尽管它们有很多限制docs.scala-lang.org/overviews/core/value-classes.html(另见@987654328 @),它们不应该干扰隐式解析。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 2013-02-26
  • 2017-07-17
  • 1970-01-01
相关资源
最近更新 更多