开发者

scala source implicit conversion from Int to RichInt

I understand in S开发者_开发问答cala that Int is implicitly converted to RichInt. Where in the source does that occur (I was browsing the Scala source, but I couldn't find it...)


Look at Predef.intWrapper(Int): RichInt

This is inherited by Predef from LowPriorityImplicits. Inherited implicits have lower priorities than non inherited ones.

Note that by browsing library source you don't really get to see the conversion. The best way to see it on small snippet is to compile it (or run it in the REPL) with the -Xprint:typer option. This will show the conversion that is inserted by the typer to make the code compile when the types don't match:

$ 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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜