我读过Scala函数(Scala另一个指南的一部分)。在那篇帖子中,他说:

方法和函数不是一回事

但他什么也没解释。他到底想说什么?


当前回答

实际上,Scala程序员只需要知道以下三条规则就可以正确使用函数和方法:

由def定义的方法和由=>定义的函数字面量是函数。在《Programming in Scala》第4版第8章第143页中定义。 函数值是可以作为任何值传递的对象。函数字面量和部分应用的函数是函数值。 如果在代码中的某个位置需要函数值,则可以省略部分应用的函数的下划线。例如:someNumber.foreach(println)

在《Scala编程》发行了四个版本之后,区分函数和函数值这两个重要概念仍然是个问题,因为所有版本都没有给出明确的解释。语言规范太复杂了。我发现上面的规则既简单又准确。

其他回答

方法和函数之间一个很大的实际区别是返回的含义。Return只从一个方法返回。例如:

scala> val f = () => { return "test" }
<console>:4: error: return outside method definition
       val f = () => { return "test" }
                       ^

从方法中定义的函数返回一个非局部返回:

scala> def f: String = {                 
     |    val g = () => { return "test" }
     | g()                               
     | "not this"
     | }
f: String

scala> f
res4: String = test

而从局部方法返回只从该方法返回。

scala> def f2: String = {         
     | def g(): String = { return "test" }
     | g()
     | "is this"
     | }
f2: String

scala> f2
res5: String = is this

在Scala 2.13中,与函数不同,方法可以接受/返回

类型参数(多态方法) 隐式参数 从属类型

然而,这些限制在dotty (Scala 3)中通过多态函数类型#4672解除了,例如,dotty版本0.23.0-RC1支持以下语法

类型参数

def fmet[T](x: List[T]) = x.map(e => (e, e))
val ffun = [T] => (x: List[T]) => x.map(e => (e, e))

隐式参数(上下文参数)

def gmet[T](implicit num: Numeric[T]): T = num.zero
val gfun: [T] => Numeric[T] ?=> T = [T] => (using num: Numeric[T]) => num.zero

从属类型

class A { class B }
def hmet(a: A): a.B = new a.B
val hfun: (a: A) => a.B = hmet

更多示例请参见tests/run/ polymorphism -functions.scala

这是Rob Norris写的一篇很棒的文章,解释了两者的区别

Scala中的方法不是值,但函数是。您可以构造一个通过η扩展(由后面的下划线触发)委托给某个方法的函数。

定义如下:

方法是用def定义的东西,值是可以赋值给val的东西

简而言之(摘自博客):

定义方法时,我们发现不能将其赋值给val。

scala> def add1(n: Int): Int = n + 1
add1: (n: Int)Int

scala> val f = add1
<console>:8: error: missing arguments for method add1;
follow this method with `_' if you want to treat it as a partially applied function
       val f = add1

还要注意add1的类型,它看起来不正常;你不能声明一个(n: Int)Int类型的变量。方法不是值。

然而,通过添加η扩展后加算符(η读作“eta”),我们可以将该方法转化为函数值。注意f的类型。

scala> val f = add1 _
f: Int => Int = <function1>

scala> f(3)
res0: Int = 4

_的效果相当于执行以下操作:我们构造一个Function1实例,委托给我们的方法。

scala> val g = new Function1[Int, Int] { def apply(n: Int): Int = add1(n) }
g: Int => Int = <function1>

scala> g(3)
res18: Int = 4

假设你有一个列表

scala> val x =List.range(10,20)
x: List[Int] = List(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)

定义一个方法

scala> def m1(i:Int)=i+2
m1: (i: Int)Int

定义一个函数

scala> (i:Int)=>i+2
res0: Int => Int = <function1>

scala> x.map((x)=>x+2)
res2: List[Int] = List(12, 13, 14, 15, 16, 17, 18, 19, 20, 21)

接受参数的方法

scala> m1(2)
res3: Int = 4

用val定义函数

scala> val p =(i:Int)=>i+2
p: Int => Int = <function1>

函数的参数是可选的

 scala> p(2)
    res4: Int = 4

scala> p
res5: Int => Int = <function1>

方法的参数是强制的

scala> m1
<console>:9: error: missing arguments for method m1;
follow this method with `_' if you want to treat it as a partially applied function

查看下面的教程,它解释了通过其他示例传递其他差异,如diff与方法Vs函数的其他示例,使用函数作为变量,创建返回函数的函数

函数不支持默认参数。做的方法。从方法转换到函数会丢失参数默认值。(Scala 2.8.1发布)