Scala:理解匿名函数语法

Scala: understanding anonymous function syntax

我正在尝试理解另一个程序员在 Scala 中编写的自定义迭代器。 我无法理解函数声明。 它们对我来说就像匿名函数,但我无法完全理解它们。

我阅读了一些有关 Scala 中的匿名函数的资料,发现此资源 [http://www.scala-lang.org/old/node/133] 很有帮助,但我仍然无法阅读上述函数并完全理解它们。

代码如下:

class MyCustomIterator(somePath: Path, someInt: Int, aMaxNumber: Int) {
      def customFilter:(Path) => Boolean = (p) => true
       // Path is from java.nio.files.Path
      def doSomethingWithPath:(Path) => Path = (p) => p
}

我想了解这些了解这些功能。 return 类型到底是什么?函数体是什么?

.

(对于第一个def)冒号之后和等号之前的部分是return类型。所以,return 类型是:

Path => Boolean

表示函数签名。

现在,将其分解,箭头左侧的项目是函数的参数。右侧是函数的 return 类型。

因此,它是 returning 一个接受 Path 和 return 一个 Boolean 的函数。在这种情况下,它是 returning 一个无论如何都会接受 Path 和 return true 的函数。

第二个 def return 是一个接受 Path 和 return 另一个 Path 的函数(与此相同的 Path例)

一个示例用法是按如下方式使用它们:

第一种方法:

iter.customFilter(myPath) //returns true

val pathFunction = iter.customFilter;
pathFunction(myPath) //returns true

第二种方法:

iter.doSomethingWithPath(myPath) //returns myPath

val pathFunction = iter.doSomethingWithPath
pathFunction(myPath) //returns myPath