Simple In Scala 1 + 2
there is only syntactic sugar over 1.+(2)
. This means that Int
has a method called +
that accepts Int
:
final class Int extends AnyVal { def +(x: Int): Int =
That is why you can use 1 +
as if it were a function. An example with a less unexpected method name:
scala> def s42(f: String => String) = f("42") s42: (f: String => String)String scala> s42("abc".concat) res0: String = abc42
BTW Technically speaking, the eta extension is also used to convert a method to a function.
source share