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

查看:120
本文介绍了def f(x:Int)= x + 1和val f =(x:Int)=> x + 1在斯卡拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Scala的新手,并且在理解这个问题时遇到了问题。为什么同一个概念有两种语法,并且它们中没有一个更有效或更短(仅从打字的角度来看,也许它们在行为上有所不同 - 这就是我所要求的)。

类似物有一个实际的区别 - 你不能转发 - 引用分配给变量的lambda,但是你可以从任何地方引用一个命名函数。如果我正确理解Scala,Scala就会混合使用这两种方法:您可以对任何变量进行前向引用(如果我错了,请纠正我)。




请注意,这个问题不是定义一个函数def和val有什么不同?



我知道 def 会在每次引用/调用 = 后评估表达式, val >只有一次。但是,这是不同的,因为 val 定义中的表达式计算为一个函数。






它也不是功能与Scala中的方法的重复。

这个问题涉及到Scala的语法,并没有直接询问函数和方法之间的区别。尽管答案在内容上可能相似,但在本网站上澄清这一点确实很有价值。

是三个主要区别(我知道的):

1。内部表示



函数表达式(又名匿名函数或lambda表达式)在生成的字节码中表示为任何函数

2。引用语法



函数和方法的引用有不同的语法。当您想将方法的引用作为参数发送到代码的其他部分时,您不能只说 foo 。你必须说 foo _ 。有了函数,你可以说 foo ,并且事情将按照预期工作。语法 foo _ 实际上将对 foo 的调用包装在一个匿名函数中。



3。泛型支持



方法支持类型参数化,函数不支持。例如,没有办法使用函数值来表示以下内容:

  def identity [A](a:A): A = a 

最接近的就是这个,但它丢失了类型信息:

  val identity =(a:Any)=> a 


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 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.


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):

1. Internal Representation

Function expressions (aka anonymous functions or lambdas) are represented in the generated bytecode as instances of any of the 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

References to functions and methods have different syntaxes. You can't just say 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

Methods support type parametrization, functions do not. For example, there's no way to express the following using a function value:

def identity[A](a: A): A = a

The closest would be this, but it loses the type information:

val identity = (a: Any) => a

这篇关于def f(x:Int)= x + 1和val f =(x:Int)=> x + 1在斯卡拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆