Scala“a”+ _.toString的行为不像“a”。+(_。toString)(Scala “a” + _.toString not behaving like “a”.+(_.toString))

据我所知,Scala中的中缀运算符用法应该等同于调用方法。 所以:

scala> "a" + 3.toString res0: java.lang.String = a3

是相同的:

scala> "a".+(3.toString) res1: java.lang.String = a3

当遇到占位符时,我偶然发现这种情况没有发生。 我正在做一些更复杂的事情,但可以将其提炼为:

scala> def x(f:(Int)=>String) = f(3) x: (f: Int => String)String scala> x("a" + _.toString) res3: String = a3

到现在为止还挺好。 但...

scala> x("a".+(_.toString)) <console>:9: error: missing parameter type for expanded function ((x$1) => x$1.toString) x("a".+(_.toString))

这里有什么区别? 我错过了什么?

霍尔迪

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

最满意答案

_占位符只能出现在其功能中最顶端的Expr中。 这意味着

(_.toString)

本身就是一个函数, "a" + some function of unknown type编译器没有多大意义。

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.

Scala“a”+ _.toString的行为不像“a”。+(_。toString)(Scala “a” + _.toString not behaving like “a”.+(_.toString))

据我所知,Scala中的中缀运算符用法应该等同于调用方法。 所以:

scala> "a" + 3.toString res0: java.lang.String = a3

是相同的:

scala> "a".+(3.toString) res1: java.lang.String = a3

当遇到占位符时,我偶然发现这种情况没有发生。 我正在做一些更复杂的事情,但可以将其提炼为:

scala> def x(f:(Int)=>String) = f(3) x: (f: Int => String)String scala> x("a" + _.toString) res3: String = a3

到现在为止还挺好。 但...

scala> x("a".+(_.toString)) <console>:9: error: missing parameter type for expanded function ((x$1) => x$1.toString) x("a".+(_.toString))

这里有什么区别? 我错过了什么?

霍尔迪

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

最满意答案

_占位符只能出现在其功能中最顶端的Expr中。 这意味着

(_.toString)

本身就是一个函数, "a" + some function of unknown type编译器没有多大意义。

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.