scala 函数参数类型和 =>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9508051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:56:06  来源:igfitidea点击:

Function parameter types and =>

scalaparametersdeclaration

提问by PrimosK

What exactly that declaration of method parameter means:

方法参数的声明究竟意味着什么:

def myFunc(param: => Int) = param

What is meaning of =>in upper definition?

=>上定义中的意思是什么?

回答by Tomasz Nurkiewicz

This is so-called pass-by-name. It means you are passing a function that should return Intbut is mostly used to implement lazy evaluation of parameters. It is somewhat similar to:

这就是所谓的pass-by-name。这意味着您正在传递一个应该返回Int但主要用于实现参数的延迟评估的函数。它有点类似于:

def myFunc(param: () => Int) = param

Here is an example. Consider an answerfunction returning some Intvalue:

这是一个例子。考虑一个answer返回一些Int值的函数:

def answer = { println("answer"); 40 }

And two functions, one taking Intand one taking Intby-name:

还有两个函数,一个是拿Int,一个是Int按名字

def eagerEval(x: Int)   = { println("eager"); x; }
def lazyEval(x: => Int) = { println("lazy");  x; }

Now execute both of them using answer:

现在使用answer以下命令执行它们:

eagerEval(answer + 2)
> answer
> eager

lazyEval(answer + 2)
> lazy
> answer

The first case is obvious: before calling eagerEval()answeris evaluated and prints "answer"string. The second case is much more interesting. We are actually passing a function to lazyEval(). The lazyEvalfirst prints "lazy"and evaluates the xparameter (actually, calls xfunction passed as a parameter).

第一种情况很明显:在调用之前eagerEval()answer评估并打印"answer"字符串。第二种情况更有趣。我们实际上是将一个函数传递给lazyEval(). 第lazyEval一个打印"lazy"并评估x参数(实际上,调用x作为参数传递的函数)。

See also

也可以看看

回答by Wilfred Springer

Just to make sure there is an answer that uses the proper term: the Scala Language Specification uses the term call-by-name:

只是为了确保有一个使用正确术语的答案:Scala 语言规范使用术语call-by-name

The type of a value parameter may be pre?xed by =>, e.g. x: => T . The type of such a parameter is then the parameterless method type => T . This indicates that the corresponding argument is not evaluated at the point of function application, but instead is evaluated at each use within the function. That is, the argument is evaluated using call-by-name.

值参数的类型可以以 => 为前缀,例如 x: => T 。这样一个参数的类型就是无参数方法 type => T 。这表明相应的参数不是在函数应用时计算的,而是在函数内的每次使用时计算的。也就是说,参数是使用call-by-name评估的。

-- Section 4.6.1 of the Scala Language Specification

-- Scala 语言规范的第 4.6.1 节

回答by mo-seph

To add to Tomasz Nurkiewicz's answer above, the difference I encounter between () => Int and => Int is that the second allows calling with bare blocks:

要添加上面 Tomasz Nurkiewicz 的回答,我在 () => Int 和 => Int 之间遇到的区别在于第二个允许使用裸块调用:

scala> def myfunc(f : () => Int ) = println("Evaluated: " + f )
myfunc: (f: () => Int)Unit

scala> def myfunc2(f : => Int ) = println("Evaluated: " + f )
myfunc2: (f: => Int)Unit

scala> myfunc({1})
<console>:9: error: type mismatch;
 found   : Int(1)
 required: () => Int
              myfunc({1})
                  ^

scala> myfunc2({1})
Evaluated: 1