scala中的函数和函数文字

时间:2016-10-19 14:31:57

标签: scala function literals function-literal

我是Scala的新手。请说出

之间的区别
    def fun( t: Int => Int):Unit = {

    def fun(t: =>Int):Unit {

    def fun(t:=>Int):Unit { (without space b/w ":" and "=>"))

1 个答案:

答案 0 :(得分:4)

def fun( t: Int => Int):Unit是一个采用单个参数t的方法。它的类型Int => Int是一个带Int的函数,并返回Int。但是,fun的返回类型为Unit

def fun(t: =>Int):Unit是一种接受call by name参数t的方法。同样,此方法的返回类型为Unit

另见What is "Call By Name"?

第二种和第三种方法没有区别。