作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在Scala中了解到,Int隐式转换为RichInt。在源代码中的何处发生(我正在浏览Scala源代码,但找不到它...)
最佳答案
看Predef.intWrapper(Int): RichInt
这是Predef
从LowPriorityImplicits
继承的。继承的隐式优先级低于未继承的隐式。
请注意,通过浏览库源代码,您实际上并不会看到转换。在小片段上查看它的最好方法是使用-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
}
关于Scala源从Int到RichInt的隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669627/
为什么 Scala 的 RichInt 扩展 Comparable[Int] 而不是 Comparable[RichInt]? 我是 Scala 的新手。在浏览 Scala 库代码时,我注意到类 Ri
这是一个简单的问题。 为什么与 Int 相关的方法不在 Int 中? 相反,Scala 费心将相关方法放入 RichInt并依靠 隐式转换 以便让它们像 Int 的方法一样工作. 何苦? 最佳答案 S
我是一名优秀的程序员,十分优秀!