Scala“a”+

编程入门 行业动态 更新时间:2024-10-06 06:52:18
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.

更多推荐

本文发布于:2023-08-07 17:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!