为了账号安全,请及时绑定邮箱和手机立即绑定

函数中小括号和大括号效果是否一样

def greeting() = {name: String} => { s"Hello $name"}
                                                  //> greeting: ()String => String
  greeting()("zhangsan")                          //> res1: String = Hello zhangsan
 
  def greetings() = (name: String) => ("Hello"+" "+name)
                                                  //> greetings: ()String => String
 
  greetings()("zhangsan")                         //> res2: String = Hello zhangsan
 
  def greet() = (name: String) => {"Hello"+" "+name}
                                                  //> greet: ()String => String
 
  greet(){"zhangsan"}                             //> res3: String = Hello zhangsan
 
  def greets(age: Int) = {name: String} => {"Hello"+" "+name+age}
                                                  //> greets: (age: Int)String => String
 
  greets(23){"zhangsan"}                          //> res4: String = Hello zhangsan23

正在回答

1 回答

经过测试,非匿名函数的参数列表不能用大括号,匿名函数的参数和函数体大括号小括号通用

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

函数中小括号和大括号效果是否一样

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信