在 Scala 中定义函数的两种方式.有什么不同? [英] Two ways of defining functions in Scala. What is the difference?
问题描述
这是一个小的 Scala 会话,它定义并尝试了一些函数:
Here is a little Scala session that defines and tries out some functions:
scala> def test1(str: String) = str + str;
test1: (str: String)java.lang.String
scala> test1("ab")
res0: java.lang.String = abab
效果很好.
scala> val test2 = test1
<console>:6: error: missing arguments for method test1 in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
val test2 = test1
^
哎呀.
scala> val test2 = test1 _
test2: (String) => java.lang.String = <function1>
scala> test2("ab")
res1: java.lang.String = abab
效果很好!
现在,我已经看到了折叠时的 _
语法(_ + _
等).所以据我了解 _
基本上意味着一个论点".所以 test1 _
基本上意味着一个带有参数的函数,它被赋予 test1
".但为什么这与 完全 不一样test1
?为什么我附加一个_
会有不同?
Now, I've seen the _
syntax when folding (_ + _
, etc). So as I understand it _
basically means "an argument". So test1 _
basically means a function with an argument, which is given to test1
". But why isn't that exactly the same as just test1
? Why is there a difference if I append a _
?
所以我一直在探索......
So I kept exploring...
scala> val test3 = (str: String) => str + str
test3: (String) => java.lang.String = <function1>
scala> test3("ab")
res2: java.lang.String = abab
scala> val test4 = test3
test4: (String) => java.lang.String = <function1>
这里无需_
即可工作!def
ed 函数和 val
ed 函数有什么区别?
Here it works without _
! What's the difference between a def
ed function, and a val
ed function?
推荐答案
def'ed 函数和 val'ed 函数没有区别:
There's no difference between a def'ed function and a val'ed function:
scala> def test1 = (str: String) => str + str
test1: (String) => java.lang.String
scala> val test2 = test1
test2: (String) => java.lang.String = <function1>
scala> val test3 = (str: String) => str + str
test3: (String) => java.lang.String = <function1>
scala> val test4 = test2
test4: (String) => java.lang.String = <function1>
看到了吗?所有这些都是函数,用 X => 表示.Y
他们拥有的类型.
See? All of these are functions, which is indicated by the X => Y
type they have.
scala> def test5(str: String) = str + str
test5: (str: String)java.lang.String
你看到一个 X =>Y
类型?如果你这样做,去看眼科医生,因为没有.这里的类型是(X)Y
,常用来表示一个方法.
Do you see an X => Y
type? If you do, go see an ophthalmologist, because there's none. The type here is (X)Y
, commonly used to denote a method.
其实test1
、test2
、test3
和test4
都是方法,返回函数.test5
是一个返回 java.lang.String
的方法.此外,test1
到 test4
不带参数(无论如何只有 test1
可以),而 test5
可以.
Actually, test1
, test2
, test3
and test4
are all methods, which return functions. test5
is a method which returns a java.lang.String
. Also, test1
through test4
do not take parameters (only test1
could, anyway), while test5
does.
所以,区别很简单.在第一种情况下,您尝试将一个方法分配给一个 val,但没有填写该方法采用的参数.所以它失败了,直到你添加了一个尾随下划线,这意味着把我的方法变成一个函数.
So, the difference is pretty simple. In the first case, you tried to assign a method to a val, but did not fill in the parameters the method take. So it failed, until you added a trailing underscore, which meant turn my method into a function.
在第二个示例中,您有一个函数,因此您无需执行任何其他操作.
In the second example you had a function, so you didn't need to do anything else.
方法不是函数,反之亦然.函数是 FunctionN
类之一的对象.方法是与对象关联的某些代码段的句柄.
A method is not a function, and vice versa. A function is an object of one of the FunctionN
classes. A method is a handle to some piece of code associated with an object.
在 Stack Overflow 上查看有关方法与函数的各种问题.
See various questions about methods vs functions on Stack Overflow.
这篇关于在 Scala 中定义函数的两种方式.有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!