小编Voi*_*BzY的帖子

在scala中从Int到Double的隐式转换不起作用

我已经编写了一些隐式代码,如下所示,我想知道为什么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
}
Run Code Online (Sandbox Code Playgroud)

输出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 …
Run Code Online (Sandbox Code Playgroud)

scala implicit-conversion scalac

11
推荐指数
1
解决办法
3137
查看次数

标签 统计

implicit-conversion ×1

scala ×1

scalac ×1