Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scala中的函数和函数文本_Scala_Function_Literals_Function Literal - Fatal编程技术网

scala中的函数和函数文本

scala中的函数和函数文本,scala,function,literals,function-literal,Scala,Function,Literals,Function Literal,我是斯卡拉的新手。请告诉我两者的区别 def fun( t: Int => Int):Unit = { 及 及 def-fun(t:Int=>Int):Unit是一个接受单个参数的方法,t。它的类型为Int=>Int,是一个函数,它接受Int,并返回Int。但是,fun的返回类型是Unit def-fun(t:=>Int):Unit是一个接受参数t的方法。同样,此方法的返回类型是Unit 我也看到了 第二种和第三种方法没有区别。谢谢你的回答。但是“def-fun(t:Int=>I

我是斯卡拉的新手。请告诉我两者的区别

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

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

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

我也看到了


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

谢谢你的回答。但是“def-fun(t:Int=>Int):Unit”是否也按名称调用?否,因为
=>
在按名称调用参数前面加了前缀。你提到的情况不是这样的。对不起,你能详细说明一下吗。当我使用def fun(t:Int=>Int):Unit运行代码并在fun中传递一个函数时,只有在使用变量“t”时才调用该函数。那么,它不是按名称调用吗?嗯,为了调用该函数,您对
Int=>Int
应用了
Int
。但是,在按名称调用示例中,即
def-fun(t:=>Int)
t
仅在调用时和每次调用时才进行计算。示例:
def-fun(t:=>Int):Int=t+t
,并调用它:
fun({println(“calling-fun”);42}
,观察
调用fun
将打印两次,并返回
84
    def fun(t: =>Int):Unit {
    def fun(t:=>Int):Unit { (without space b/w ":" and "=>"))