def f(x:Int)= x + 1和val f =(x:Int)=> x + 1在斯卡拉 [英] Practical difference between def f(x: Int) = x+1 and val f = (x: Int) => x+1 in Scala
问题描述
我是Scala的新手,并且在理解这个问题时遇到了问题。为什么同一个概念有两种语法,并且它们中没有一个更有效或更短(仅从打字的角度来看,也许它们在行为上有所不同 - 这就是我所要求的)。 请注意,这个问题不是定义一个函数def和val有什么不同? 我知道 它也不是功能与Scala中的方法的重复。 这个问题涉及到Scala的语法,并没有直接询问函数和方法之间的区别。尽管答案在内容上可能相似,但在本网站上澄清这一点确实很有价值。
def
会在每次引用/调用 val
定义中的表达式计算为一个函数。
是三个主要区别(我知道的):
1。内部表示
函数表达式(又名匿名函数或lambda表达式)在生成的字节码中表示为任何 函数和方法的引用有不同的语法。当您想将方法的引用作为参数发送到代码的其他部分时,您不能只说 方法支持类型参数化,函数不支持。例如,没有办法使用函数值来表示以下内容: 最接近的就是这个,但它丢失了类型信息: I'm new to Scala and I'm having a problem understanding this. Why are there two syntaxes for the same concept, and none of them more efficient or shorter at that (merely from a typing standpoint, maybe they differ in behavior - which is what I'm asking). In Go the analogues have a practical difference - you can't forward-reference the lambda assigned to a variable, but you can reference a named function from anywhere. Scala blends these two if I understand it correctly: you can forward-reference any variable (please correct me if I'm wrong). Please note that this question is not a duplicate of What is the difference between "def" and "val" to define a function. I know that It is also not a duplicate of Functions vs methods in Scala. This question concerns the syntax of Scala, and is not asking about the difference between functions and methods directly. Even though the answers may be similar in content, it's still valuable to have this exact point cleared up on this site. There are three main differences (that I know of): Function expressions (aka anonymous functions or lambdas) are represented in the generated bytecode as instances of any of the References to functions and methods have different syntaxes. You can't just say Methods support type parametrization, functions do not. For example, there's no way to express the following using a function value: The closest would be this, but it loses the type information:
这篇关于def f(x:Int)= x + 1和val f =(x:Int)=> x + 1在斯卡拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!函数$ c $性状。这意味着函数表达式也是对象。另一方面,方法定义是JVM上的第一类公民,并且具有特殊的字节码表示。如果没有分析,这对性能的影响很难说明。
2。引用语法
foo
。你必须说 foo _
。有了函数,你可以说 foo
,并且事情将按照预期工作。语法 foo _
实际上将对 foo
的调用包装在一个匿名函数中。
3。泛型支持
def identity [A](a:A): A = a
val identity =(a:Any)=> a
def
evaluates the expression after =
each time it is referenced/called, and val
only once. But this is different because the expression in the val
definition evaluates to a function.
1. Internal Representation
Function
traits. This means that function expressions are also objects. Method definitions, on the other hand, are first class citizens on the JVM and have a special bytecode representation. How this impacts performance is hard to tell without profiling.2. Reference Syntax
foo
when you want to send the reference of a method as an argument to some other part of your code. You'll have to say foo _
. With functions you can just say foo
and things will work as intended. The syntax foo _
is effectively wrapping the call to foo
inside an anonymous function.3. Generics Support
def identity[A](a: A): A = a
val identity = (a: Any) => a