隐式转换和null

Dr *_*Wit 8 scala string-concatenation operators

以下代码

import scala.language.implicitConversions

object myObj {
  implicit def nullToInt(x: Null) = 0

  def main(args: Array[String]): Unit = {
    val x = 1 + null
    val y = 1 + nullToInt(null)

    println(x + " " + y)
  }
}
Run Code Online (Sandbox Code Playgroud)

给出以下结果

1null 1
Run Code Online (Sandbox Code Playgroud)

我期待两个val都是Int并且等于1.

显然,第一个val是String,等于"1null".

Xprint:typer 表明源代码已翻译成

package <empty> {
  import scala.language.implicitConversions;
  object myObj extends scala.AnyRef {
    def <init>(): myObj.type = {
      myObj.super.<init>();
      ()
    };
    implicit def nullToInt(x: Null): Int = 0;
    def main(args: Array[String]): Unit = {
      val x: String = 1.+(null);
      val y: Int = 1.+(myObj.this.nullToInt(null));
      scala.Predef.println(x.+(" ").+(y))
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

int没有符号方法接受null

scala> 10+null
res0: String = 10null

scala> 10*null
<console>:12: error: overloaded method value * with alternatives:
  (x: Double)Double <and>
  (x: Float)Float <and>
  (x: Long)Long <and>
  (x: Int)Int <and>
  (x: Char)Int <and>
  (x: Short)Int <and>
  (x: Byte)Int
 cannot be applied to (Null)
       10*null
         ^

scala> 10-null
<console>:12: error: overloaded method value - with alternatives:
  (x: Double)Double <and>
  (x: Float)Float <and>
  (x: Long)Long <and>
  (x: Int)Int <and>
  (x: Char)Int <and>
  (x: Short)Int <and>
  (x: Byte)Int
 cannot be applied to (Null)
       10-null
         ^
Run Code Online (Sandbox Code Playgroud)

我假设"1"和"null"都转换为String而不是应用隐式nullToInt.有人可以解释编译器如何提出这个问题吗?使用了什么逻辑/工作流程?

另一个问题是是否有办法启用implcit nullToInt?

PS.我不是在谈论这里的最佳做法.作为学术兴趣,请随意考虑问题.

Dr *_*Wit 3

我会尝试回答我自己的问题。

这个主题有点误导,实际上 val x 的表达式根本没有应用隐式转换。Null是 Int 的子类型String,并且 Int 有方法abstract def +(x: String): String,因此它也可以应用于 Null。

的输出也证实了这一点Xprint:typer,因为它应该显示所有隐式转换,并且显然它没有显示 x 表达式的任何内容。

并回答“是否有办法启用隐式 nullToInt”的问题,启用它的唯一方法是在这种情况下显式指定,因为当代码在没有隐式的情况下成功编译时,编译器不会考虑使用任何隐式。