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

来源:5-1 Scala 函数与匿名函数

潜心_守道

2019-10-31 14:57

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回答

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

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

Scala程序设计—基础篇

Scala基础编程入门视频教程,能够编写简单的Scala代码

37007 学习 · 77 问题

查看课程

相似问题