我还在学习Scala,当我在Koans中遇到一个例子时,我无法理解它为什么会起作用:
var foo : Any = "foo"
println(foo + "bar")
Run Code Online (Sandbox Code Playgroud)
任何没有+方法
Dyl*_*lan 22
对象中存在隐式转换scala.Predef
:
implicit def any2stringadd(x: Any): StringAdd
Run Code Online (Sandbox Code Playgroud)
StringAdd定义了一个+
运算符/方法:
def +(other: String) = String.valueOf(self) + other
Run Code Online (Sandbox Code Playgroud)
此外,由于scala.Predef始终在范围内,因此隐式转换将始终有效.
归档时间: |
|
查看次数: |
747 次 |
最近记录: |