12

我已经编写了一些隐式代码,如下所示,我想知道为什么i2d不调用函数隐式对话。

object Test {
  implicit def i2d(x: Int): Double = {
    println("foo")
    x.toDouble
  }

  implicit def d2i(x: Double): Int = {
    x.toInt
  }

  val x: Int = 10
  val y: Double = x
  val z: Int = 3.5
}

输出_scalac -Xprint:typer Test.scala

// Test.scala
[[syntax trees at end of typer]] 
package <empty> {
  object Test extends scala.AnyRef {
    def <init>(): Test.type = {
      Test.super.<init>();
      ()
    };
    implicit def i2d(x: Int): Double = {
      scala.this.Predef.println("foo");
      x.toDouble
    };
    implicit def d2i(x: Double): Int = x.toInt;
    private[this] val x: Int = 10;
    <stable> <accessor> def x: Int = Test.this.x;
    private[this] val y: Double = Test.this.x.toDouble;
    <stable> <accessor> def y: Double = Test.this.y;
    private[this] val z: Int = Test.this.d2i(3.5);
    <stable> <accessor> def z: Int = Test.this.z
  }
}

眼镜

  • scalac 版本是 2.11.8。
4

2 回答 2

15

这比我想象的要复杂得多。

起初,我认为这是因为 Scala 是如何解析隐式的。不知何故,我认为这隐含在Int.scala被优先考虑。优先级的规则通常很直观,但是对于像这样的边缘情况,我们参考6.26.3 重载解决方案。让我感到困惑的是,调用int2double不存在 - 它已经内联到.toDouble!!

挖得更多,似乎有一个关于值类型的极端情况,适用于从Intto的转换Double。称为弱一致性的东西决定了我们如何转换Byte -> Short -> Int -> Long -> Float -> Double。所以,总而言之,我认为你不能推翻这个内置转换......

这种转换称为数值扩展

数值加宽

如果e具有弱符合预期类型的​​原始数字类型,则使用数字转换方法之一将其扩展为预期类型toShort, toChar, toInt, toLong, toFloat...toDouble

编辑

此外,如果有人想知道为什么会这样,来自Martin Odersky(这是一个旧链接 - 不要相信这里所说的一般),如果我们没有这些额外的特殊问题,我们会遇到常见问题转换:

scala-hypothetical> val x = List(1, 2.0) 
x: List[AnyVal]

不是很直观...

于 2016-08-16T05:13:16.297 回答
3

查看 Int 伴随对象中的最后一行。我认为这与视图的概念有关:

object Int extends AnyValCompanion {
  /** The smallest value representable as a Int.
   */
  final val MinValue = java.lang.Integer.MIN_VALUE

  /** The largest value representable as a Int.
   */
  final val MaxValue = java.lang.Integer.MAX_VALUE

  /** Transform a value type into a boxed reference type.
   *
   *  @param  x   the Int to be boxed
   *  @return     a java.lang.Integer offering `x` as its underlying value.
   */
  def box(x: Int): java.lang.Integer = java.lang.Integer.valueOf(x)

  /** Transform a boxed type into a value type.  Note that this
   *  method is not typesafe: it accepts any Object, but will throw
   *  an exception if the argument is not a java.lang.Integer.
   *
   *  @param  x   the java.lang.Integer to be unboxed.
   *  @throws     ClassCastException  if the argument is not a java.lang.Integer
   *  @return     the Int resulting from calling intValue() on `x`
   */
  def unbox(x: java.lang.Object): Int = x.asInstanceOf[java.lang.Integer].intValue()

  /** The String representation of the scala.Int companion object.
   */
  override def toString = "object scala.Int"

  /** Language mandated coercions from Int to "wider" types.
   */
  implicit def int2long(x: Int): Long = x.toLong
  implicit def int2float(x: Int): Float = x.toFloat
  implicit def int2double(x: Int): Double = x.toDouble
}

在此处输入图像描述

另请参阅:Scala 在哪里寻找隐式?

编辑

根据@som-snytt 的评论:

scala -Ywarn-numeric-widen
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_91).
Type in expressions for evaluation. Or try :help.

scala> object Test {
     |   implicit def i2d(x: Int): Double = {
     |     println("foo")
     |     x.toDouble
     |   }
     |
     |   implicit def d2i(x: Double): Int = {
     |     x.toInt
     |   }
     |
     |   val x: Int = 10
     |   val y: Double = x
     |   val z: Int = 3.5
     | }
<console>:22: warning: implicit numeric widening
         val y: Double = x
                         ^

另外,添加到@Alec关于扩大的答案: http ://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#value-conversions

来自:http ://www.scala-lang.org/files/archive/spec/2.11/03-types.html#weak-conformance

弱一致性 在某些情况下,Scala 使用更一般的一致性关系。类型 >SS 弱符合类型 TT,写为 S<:wTS<:wT,如果 S<:TS<:T 或 >SS 和 TT 都是原始数字类型,并且 SS 在以下 >排序中位于 TT 之前。

字节 <:w<:w 短

短 <:w<:w 整数

字符 <:w<:w 整数

整数 <:w<:w 长

长 <:w<:w 浮点数

浮点 <:w<:w 双精度

弱最小上界是关于弱一致性的最小上界。

于 2016-08-16T05:08:27.093 回答