更新时间:2023-02-26 17:08:23
Scala 区分以下几点:
Scala distinguishes between the following things:
这些都不是等价的,尽管为方便起见,Scala 允许您省略空参数列表.(顺便说一句,两个空参数列表也不一样.)
None of these are equivalent, although as a convenience Scala allows you to elide empty parameter lists. (Incidentally, two empty parameter lists are also not the same.)
所以,即使 Unit
写成 ()
,这与函数参数 parens ()不 相同/code> 用于函数或方法.相反,将
()
视为 Tuple0
.
So, even though Unit
is written ()
, this is not the same as the function argument parens ()
for a function or method. Instead, think of ()
as a Tuple0
.
所以,如果你说 f: Unit =>int
,你的意思是f接受一个参数,但它是一个非常无聊的参数,因为它是Unit
,它必须始终是同一个无聊的Tuple0
值()
".你正在写的是 f: (Unit) => 的缩写.Int
.
So, if you say f: Unit => Int
, what you mean is "f takes one parameter, but it's a really boring parameter because it is Unit
, which must always be the same boring Tuple0
value ()
". What you're writing is really short for f: (Unit) => Int
.
如果你说 f: () =>Int
,那么你的意思是f 不接受任何参数并产生一个 Int
".
If you say f: () => Int
, then you mean that "f takes no parameters and produces an Int
".
如果你说 f: =>Int
,那么您的意思是延迟执行任何生成 Int
值的语句,直到我们在此代码中使用它(并每次重新评估它)".从功能上讲,这最终与 f: () =>; 基本相同.Int
(并且在内部被转换为相同的 Function0
类),但它有不同的用法,大概是为了允许更紧凑的闭包形式(你总是省略 =>
在调用代码中).
If you say f: => Int
, then you mean that "delay the execution of whatever statement produces an Int
value until we use it in this code (and re-evaluate it each time)". Functionally, this ends up being basically the same as f: () => Int
(and internally is converted into the same Function0
class), but it has a different usage, presumably to allow for a more compact form of closures (you always omit the =>
in the calling code).