As far as I know, the infix operator usage in Scala should be equivalent to the invocation of a method. So:
scala> "a" + 3.toString
res0: java.lang.String = a3
Is the same as:
scala> "a".+(3.toString)
res1: java.lang.String = a3
I came across an occasion where this is not happening, when there is a placeholder. I was doing something more complex, but it can be distilled to:
scala> def x(f:(Int)=>String) = f(3)
x: (f: Int => String)String
scala> x("a" + _.toString)
res3: String = a3
So far so good. But...
scala> x("a".+(_.toString))
<console>:9: error: missing parameter type for expanded function ((x$1) => x$1.toString)
x("a".+(_.toString))
What's the difference here? What am I missing?
Jordi
The _
placeholder can only appear at the topmost Expr
in its function. That means
(_.toString)
is itself a function, and "a" + some function of unknown type
doesn't make much sense to the compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With