问答详情
源自:5-1 Scala 函数与匿名函数

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

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

提问者:潜心_守道 2019-10-31 14:57

个回答

  • 潜心_守道
    2019-10-31 14:58:35

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