为什么不同风格的方法声明在scala中会产生不同的结果
why declaration of methods in different style different results in scala
下面是两种声明方式不同的方法。两者都在做同样的工作。我想知道,为什么
- 检查函数类型需要不同的语法(黄色
块)
- 调用函数需要不同的语法(绿色块)
- 两种方法在 scala repl 上的声明给出了不同的结果
(红色方块)
Also, please suggest, which one is the preferred way to declare
methods or are there any special use cases for both the styles of
method declaration?
编辑 1:
以下是屏幕截图中的命令:-
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def add(x:Int, y :Int): Int = {x+y}
add: (x: Int, y: Int)Int
scala> def sum = (x:Int, y:Int) => {x+y}
sum: (Int, Int) => Int
scala> :t add
<console>:12: error: missing arguments for method add;
follow this method with `_' if you want to treat it as a partially applied function
add
^
scala> :t add(_, _)
(Int, Int) => Int
scala> :t sum
(Int, Int) => Int
scala> add
<console>:12: error: missing arguments for method add;
follow this method with `_' if you want to treat it as a partially applied function
add
^
scala> add(_, _)
res1: (Int, Int) => Int = <function2>
scala> sum
res2: (Int, Int) => Int = <function2>
编辑 2:
@Shadowlands
我已经阅读了 dzone,上面写着 "when a function is expected but a method is provided, it will be automatically converted into a function. This is called the ETA expansion."。
现在,如果 ETA 负责将您的方法转换为函数。是否真的需要使用 sum 样式,因为它看起来像是 Function<> object 的额外开销。
这就是 REPL 的实现方式。您键入的代码包含在 object
中。因此,您的 def add
成为该对象的方法,而 def sum
是 returns Function
的方法。当您像这样寻址 add
时:add(_, _)
您正在寻址方法,而当您将 sum
寻址为 sum
时,您正在寻址 sum 的执行结果,即函数。
描述了方法和函数之间的差异here。
PS。尝试检查 sum _
的类型
简单地说,add
是一个接受两个 Int
参数和 returns 一个 Int
结果的函数,而 sum
是一个 0 参数函数 returns 一个新函数,它依次接受两个 Int
参数和 returns 一个 Int
结果。实际上,sum
可以很容易地定义为:
def sum = add _
至于用什么方式来定义你的函数,这取决于它是如何被使用的。通常,大多数时候使用 add
样式,因为它更容易理解和使用。但是,如果您要将该函数作为参数传递给另一个函数,那么 sum
样式的公式可能会更方便,并且可以传达更多关于如何使用该函数的意图。
下面是两种声明方式不同的方法。两者都在做同样的工作。我想知道,为什么
- 检查函数类型需要不同的语法(黄色 块)
- 调用函数需要不同的语法(绿色块)
- 两种方法在 scala repl 上的声明给出了不同的结果 (红色方块)
Also, please suggest, which one is the preferred way to declare methods or are there any special use cases for both the styles of method declaration?
编辑 1:
以下是屏幕截图中的命令:-
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def add(x:Int, y :Int): Int = {x+y}
add: (x: Int, y: Int)Int
scala> def sum = (x:Int, y:Int) => {x+y}
sum: (Int, Int) => Int
scala> :t add
<console>:12: error: missing arguments for method add;
follow this method with `_' if you want to treat it as a partially applied function
add
^
scala> :t add(_, _)
(Int, Int) => Int
scala> :t sum
(Int, Int) => Int
scala> add
<console>:12: error: missing arguments for method add;
follow this method with `_' if you want to treat it as a partially applied function
add
^
scala> add(_, _)
res1: (Int, Int) => Int = <function2>
scala> sum
res2: (Int, Int) => Int = <function2>
编辑 2:
@Shadowlands
我已经阅读了 dzone,上面写着 "when a function is expected but a method is provided, it will be automatically converted into a function. This is called the ETA expansion."。
现在,如果 ETA 负责将您的方法转换为函数。是否真的需要使用 sum 样式,因为它看起来像是 Function<> object 的额外开销。
这就是 REPL 的实现方式。您键入的代码包含在 object
中。因此,您的 def add
成为该对象的方法,而 def sum
是 returns Function
的方法。当您像这样寻址 add
时:add(_, _)
您正在寻址方法,而当您将 sum
寻址为 sum
时,您正在寻址 sum 的执行结果,即函数。
描述了方法和函数之间的差异here。
PS。尝试检查 sum _
简单地说,add
是一个接受两个 Int
参数和 returns 一个 Int
结果的函数,而 sum
是一个 0 参数函数 returns 一个新函数,它依次接受两个 Int
参数和 returns 一个 Int
结果。实际上,sum
可以很容易地定义为:
def sum = add _
至于用什么方式来定义你的函数,这取决于它是如何被使用的。通常,大多数时候使用 add
样式,因为它更容易理解和使用。但是,如果您要将该函数作为参数传递给另一个函数,那么 sum
样式的公式可能会更方便,并且可以传达更多关于如何使用该函数的意图。