scala源从Int到RichInt的隐式转换

Jef*_*rey 9 int scala implicit-conversion

我在Scala中理解Int被隐式转换为RichInt.在源代码中发生了什么(我正在浏览Scala源代码,但我找不到它......)

huy*_*hjl 10

看着 Predef.intWrapper(Int): RichInt

这是继承PredefLowPriorityImplicits.继承的implicits优先级低于非继承的implicits.

请注意,通过浏览库源,您无法真正看到转换.在小片段上看到它的最好方法是使用该-Xprint:typer选项编译它(或在REPL中运行它).这将显示typer插入的转换,以便在类型不匹配时编译代码:

$ 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
}
Run Code Online (Sandbox Code Playgroud)