不带参数的函数,以单位作为 Scala 中的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2774516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Functions without arguments, with unit as argument in scala
提问by scout
def foo(x: Int, f: Unit => Int) = println(f())
foo(2, { Unit => 3 + 4 })
// case 1
def loop: Int = 7
foo(2, loop) // does not compile
changing loop to
// case 2
def loop(): Int = 7
foo(2, loop) // does not compile
changing loop to
// case 3
def loop(x: Unit): Int = 7 // changing according to Don's Comments
foo(2, loop) // compiles and works fine
Shouldn't case 1and case 2also work? Why are they not working?
不宜case 1和case 2还工作吗?为什么他们不工作?
Defining foo as
将 foo 定义为
def foo(x: Int, y: () => Int)
then case 2works but not case 1.
然后case 2工作但不是case 1。
Arent they all supposed to work, defining the functions either way?
他们不是都应该工作,以任何一种方式定义功能吗?
Also I think () => Intin foo is a bad style, y:=> Intdoes not work. Comments?
另外我认为() => Intfoo 是一种糟糕的风格,y:=> Int不起作用。评论?
回答by Rex Kerr
Scala distinguishes between the following things:
Scala 区分以下几点:
- Functions/methods with no parameter lists("by-name parameter" if a function)
- Functions with one empty parameter list
- Functions with one parameter of type Unit
- 没有参数列表的函数/方法(如果是函数,则为“按名称参数”)
- 具有一个空参数列表的函数
- 带有一个 Unit 类型参数的函数
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.)
这些都不是等价的,尽管为方便起见,Scala 允许您省略空参数列表。(顺便说一下,两个空参数列表也不一样。)
So, even though Unitis written (), this is notthe same as the function argument parens ()for a function or method. Instead, think of ()as a Tuple0.
因此,即使Unit是写(),这是不一样的函数参数括号()的函数或方法。相反,将其()视为Tuple0.
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 Tuple0value ()". What you're writing is really short for f: (Unit) => Int.
所以,如果你说f: Unit => Int,你的意思是“f 采用一个参数,但它是一个非常无聊的参数,因为它是Unit,它必须始终是相同的无聊Tuple0值()”。你写的东西真的很短f: (Unit) => Int。
If you say f: () => Int, then you mean that "f takes no parameters and produces an Int".
如果你说f: () => Int,那么你的意思是“f 不接受任何参数并产生一个Int”。
If you say f: => Int, then you mean that "delay the execution of whatever statement produces an Intvalue 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 Function0class), but it has a different usage, presumably to allow for a more compact form of closures (you always omit the =>in the calling code).
如果你说f: => Int,那么你的意思是“延迟任何语句的执行产生一个Int值,直到我们在这段代码中使用它(并且每次都重新评估它)”。在功能上,这最终与f: () => Int(并且在内部转换为同一个Function0类)基本相同,但它有不同的用法,大概是为了允许更紧凑的闭包形式(您总是=>在调用代码中省略)。
回答by Eastsun
()=>Int is Function0[Int] while Unit=>Int is Function1[Unit,Int]
()=>Int 是 Function0[Int] 而 Unit=>Int 是 Function1[Unit,Int]
scala> val function0: () => Int = () => 5
function0: () => Int = <function0>
scala> val function1: Unit => Int = u => 5
function1: (Unit) => Int = <function1>
scala> function0()
res0: Int = 5
scala> function1("anything")
res1: Int = 5
scala> function1(100)
res2: Int = 5
scala>
Also note that () is an object of Unit
还要注意 () 是 Unit 的对象
scala> function1(())
res11: Int = 5
scala> function1 ()
res12: Int = 5
scala> function1()
res13: Int = 5
scala> val unit = ()
unit: Unit = ()
scala> function1(unit)
res15: Int = 5
scala> function1 apply unit
res16: Int = 5
scala>
回答by Don Mackenzie
In case 1 and 2 above, the return value of looprather than loopitself is type checked for the second argument to fooand fails: Int != Unit => Int
在上面的情况 1 和 2 中,对循环的返回值而不是循环本身进行类型检查以获取foo的第二个参数并失败:Int != Unit => Int
The change to loophas a typo.
对循环的更改有一个错字。

![Scala 创建列表[Int]](/res/img/loading.gif)