scala() vs {}参数调用的定义
scala () vs {} definition of parameter invocation
在 Coursera 课程 "Functional Programming Principles in Scala" 第 1 周示例作业 (https://www.coursera.org/learn/progfun1/programming/xIz9O/example-assignment) Martin Ordersky 在他的一条评论中提到(在 ListsSuites.scala 测试的第 44 行)
* In Scala, it is allowed to pass an argument to a method using the block
* syntax, i.e. `{ argument }` instead of parentheses `(argument)`.
所以在 Scala 中分配一个函数定义时是否等同于说:
def foo = ()
和
def foo = {}
那是定义一个 method/def - 调用 def
时引用的注释
scala> def foo(arg1: Int) = arg1 + 1
foo: foo[](val arg1: Int) => Int
scala> foo(1) // round parenthesis
res0: Int = 2
scala> foo{1} // bracket parenthesis
res1: Int = 2
另请注意,def 的主体不需要括在括号中。因此,使用您的示例,以下所有内容都是等效的:
def foo = (???) // ??? means implementation not yet defined
def foo = {???}
def foo = ???
具体针对您的情况:是的,它们都会导致 foo: Unit
在很多情况下,由于都对表达式求值,它们会产生相同的效果,但严格来说它们是不同的。 {}
是一个空块,它的计算结果为 Unit
因为它(不存在)最后一条语句的计算结果为 Unit
,而 ()
是 Unit
类型的默认值.
这个问题已经出现long ago, and imo the key point regarding your particular question is that you cannot use the {}
syntax with functions that take more than 1 argument (non-curried). Extensive coverage of other cases can be found here。通常这两者是不同的;他们只是经常碰巧做同样的事情。
{}
或 ({})
指的是代码块。
{
statement1
statement2
statement3
}
最后一条语句将被返回。
scala> val x = {
| val x = 4
| x + 1
| }
x: Int = 5
而()
是一行表达式。
{} 和 () 何时可以互换
1.invoking 一个只有一个参数的方法
scala> def printlnme(x: String) = { println(x)}
printlnme: (x: String)Unit
scala> printlnme("using parenthesis")
using parenthesis
scala> printlnme{"using braces"}
using braces
不适用于具有多个参数的方法,
scala> def add(x: Int, y: Int) = { x + y }
add: (x: Int, y: Int)Int
//can also be (x + y) because one line expression
//scala> def add(x: Int, y: Int) = ( x + y )
//add: (x: Int, y: Int)Int
scala> add(1, 2)
res3: Int = 3
scala> add{1, 2}
<console>:1: error: ';' expected but ',' found.
foo{1, 2}
^
2.one行表达式
在这个例子中我只打印输入。
scala> List(1, 2, 3).foreach { x => println(x) }
1
2
3
scala> List(1, 2, 3).foreach ( x => println(x) )
1
2
3
或者说一行 map
函数。
scala> List(1, 2, 3) map { _ * 3}
res11: List[Int] = List(3, 6, 9)
scala> List(1, 2, 3) map ( _ * 3 )
res12: List[Int] = List(3, 6, 9)
但是()
单独不能与多行语句一起使用
scala> :paste
List(1, 2, 3) foreach ( x =>
val y = x * 28
println(y)
)
<console>:2: error: illegal start of simple expression
val y = x * 28
^
您仍然需要 ({})
才能对多行使用括号。
scala> :paste
List(1, 2, 3) foreach ( x => {
val y = x * 28
println(y)
})
28
56
84
给你一个简单的回答,两者都是内存块的表示,只是在块可以容纳多少方面有所不同。
{} 用于多行定义
Seq(1, 2, 3, 4, 5).map { x =>
x + 1
x - 1
}
() 用于单行定义
Seq(1, 2, 3, 4, 5).map(x =>
x + 1
x -1)
上面的代码会给你编译器错误,因为它只能容纳单行。但你也可以在 ()[=28= 中使用 {} ]容纳多行如下
Seq(1, 2, 3, 4, 5).map(x => {
x + 1
x - 1
})
在 Coursera 课程 "Functional Programming Principles in Scala" 第 1 周示例作业 (https://www.coursera.org/learn/progfun1/programming/xIz9O/example-assignment) Martin Ordersky 在他的一条评论中提到(在 ListsSuites.scala 测试的第 44 行)
* In Scala, it is allowed to pass an argument to a method using the block
* syntax, i.e. `{ argument }` instead of parentheses `(argument)`.
所以在 Scala 中分配一个函数定义时是否等同于说:
def foo = ()
和
def foo = {}
那是定义一个 method/def - 调用 def
时引用的注释scala> def foo(arg1: Int) = arg1 + 1
foo: foo[](val arg1: Int) => Int
scala> foo(1) // round parenthesis
res0: Int = 2
scala> foo{1} // bracket parenthesis
res1: Int = 2
另请注意,def 的主体不需要括在括号中。因此,使用您的示例,以下所有内容都是等效的:
def foo = (???) // ??? means implementation not yet defined
def foo = {???}
def foo = ???
具体针对您的情况:是的,它们都会导致 foo: Unit
在很多情况下,由于都对表达式求值,它们会产生相同的效果,但严格来说它们是不同的。 {}
是一个空块,它的计算结果为 Unit
因为它(不存在)最后一条语句的计算结果为 Unit
,而 ()
是 Unit
类型的默认值.
这个问题已经出现long ago, and imo the key point regarding your particular question is that you cannot use the {}
syntax with functions that take more than 1 argument (non-curried). Extensive coverage of other cases can be found here。通常这两者是不同的;他们只是经常碰巧做同样的事情。
{}
或 ({})
指的是代码块。
{
statement1
statement2
statement3
}
最后一条语句将被返回。
scala> val x = {
| val x = 4
| x + 1
| }
x: Int = 5
而()
是一行表达式。
{} 和 () 何时可以互换
1.invoking 一个只有一个参数的方法
scala> def printlnme(x: String) = { println(x)}
printlnme: (x: String)Unit
scala> printlnme("using parenthesis")
using parenthesis
scala> printlnme{"using braces"}
using braces
不适用于具有多个参数的方法,
scala> def add(x: Int, y: Int) = { x + y }
add: (x: Int, y: Int)Int
//can also be (x + y) because one line expression
//scala> def add(x: Int, y: Int) = ( x + y )
//add: (x: Int, y: Int)Int
scala> add(1, 2)
res3: Int = 3
scala> add{1, 2}
<console>:1: error: ';' expected but ',' found.
foo{1, 2}
^
2.one行表达式
在这个例子中我只打印输入。
scala> List(1, 2, 3).foreach { x => println(x) }
1
2
3
scala> List(1, 2, 3).foreach ( x => println(x) )
1
2
3
或者说一行 map
函数。
scala> List(1, 2, 3) map { _ * 3}
res11: List[Int] = List(3, 6, 9)
scala> List(1, 2, 3) map ( _ * 3 )
res12: List[Int] = List(3, 6, 9)
但是()
单独不能与多行语句一起使用
scala> :paste
List(1, 2, 3) foreach ( x =>
val y = x * 28
println(y)
)
<console>:2: error: illegal start of simple expression
val y = x * 28
^
您仍然需要 ({})
才能对多行使用括号。
scala> :paste
List(1, 2, 3) foreach ( x => {
val y = x * 28
println(y)
})
28
56
84
给你一个简单的回答,两者都是内存块的表示,只是在块可以容纳多少方面有所不同。
{} 用于多行定义
Seq(1, 2, 3, 4, 5).map { x =>
x + 1
x - 1
}
() 用于单行定义
Seq(1, 2, 3, 4, 5).map(x =>
x + 1
x -1)
上面的代码会给你编译器错误,因为它只能容纳单行。但你也可以在 ()[=28= 中使用 {} ]容纳多行如下
Seq(1, 2, 3, 4, 5).map(x => {
x + 1
x - 1
})