【问题标题】:scala source implicit conversion from Int to RichIntscala 源从 Int 到 RichInt 的隐式转换
【发布时间】:2011-10-06 01:54:38
【问题描述】:

我理解在 Scala 中 Int 被隐式转换为 RichInt。这发生在源代码的哪个位置(我正在浏览 Scala 源代码,但找不到...)

【问题讨论】:

    标签: scala int implicit-conversion


    【解决方案1】:

    Predef.intWrapper(Int): RichInt

    这是由PredefLowPriorityImplicits 继承的。继承隐含的优先级低于非继承隐含的优先级。

    请注意,通过浏览库源,您并不能真正看到转换。在 small sn-p 上查看它的最佳方法是使用 -Xprint:typer 选项编译它(或在 REPL 中运行它)。这将显示类型不匹配时由类型器插入以使代码编译的转换:

    $ scala -Xprint:typer
    
    scala> 3.abs
    [[syntax trees at end of typer]]// Scala source: <console>
    // stuff removed
            private[this] val res0: Int = scala.this.Predef.intWrapper(3).abs;
    // more stuff removed
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2018-09-25
      • 2013-05-01
      相关资源
      最近更新 更多