命名方法和命名函数的定义是什么?
我读过问题 Scala 中方法和函数之间的差异 和许多有关方法和函数之间差异的文章。我有一种感觉,“方法”只是一个“命名函数”,定义为类、特征或对象中的方法。 “函数”代表这些文章中的“匿名函数”或“函数文字”或“函数对象”之类的东西。证据可以在《Scala 编程》一书中找到 http://www.artima.com/shop/programming_in_scala_2ed ,第 141 页,第 8.1 节,“定义函数的最常见方法是作为某个对象的成员。这样的函数称为方法。”
但是,当我检查 Scala 语言参考 http://www.scala-lang .org/docu/files/ScalaReference.pdf,有命名方法之类的概念。在第 91 页,第 6.20 节返回表达式中:“返回表达式 return e 必须出现在某些封闭命名的主体内 方法或函数。”您还可以在同一页面和其他地方找到术语“命名函数”。
所以我的问题是,在Scala中,方法、命名方法和命名函数指的是同一个概念吗?你从哪里得到的命名函数的定义?
代码List(1, 2).map(_ + 1)
中,原来的表达式_ + 1
是一个命名方法,那么该方法转换成函数是什么样的,匿名函数,函数对象,命名函数?
在我的理解中,Scala 只有两种类型的函数:作为方法的命名函数;作为函数字面量的匿名函数,将其编译为特征 FunctionN 的函数对象。在纯面向对象的 Scala 世界中使用。
但是,对于上面代码中的 _ + 1
这样的常规命名函数/方法,为什么 Scala 将其转换为另一个函数对象呢?
I have read the question Difference between method and function in Scala and many articles about differences between method and function. I got a feeling that a 'method' is just a "named function" defined as a method in a class, a trait or an object. A 'function' represents things like the "anonymous function" or "function literal" or "function object" in those articles. An evidence can be found in the book Programming in Scala http://www.artima.com/shop/programming_in_scala_2ed , page 141, section 8.1, "The most common way to define a function is as a member of some object. Such a function is called a method."
However, when I checked the Scala Language Reference http://www.scala-lang.org/docu/files/ScalaReference.pdf, there are concepts like named method. In page 91, Section 6.20 Return expressions: "A return expression return e must occur inside the body of some enclosing named
method or function." You can also find the term "named function" in the same page and other places.
So my question is, in Scala, do method, named method, and named function refer to the same concept? Where do you get the definition of named function?
In code List(1, 2).map(_ + 1)
, the original expression _ + 1
is a named method, then the method is converted into a function. What kind of function, anonymous function, function object, named function?
In my understanding, Scala only has two types of function: a named function that is a method; an anonymous function that is a function literal. Function literal is compiled into a function object of trait FunctionN for it to be used in the pure object-oriented world of Scala.
However, for a regular named funciton/method such as _ + 1
in the above code, why does Scala transform it into another function object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在语言级别,只有两个概念,
方法是 Scala 的基本构建块。方法总是被命名。方法存在于类或特征中。方法是 JVM 原生的构造,因此在 Scala 和 Java 中是相同的。 Scala 中的方法(与函数不同)可能具有特殊功能:它们可以通过类型参数进行抽象,它们的参数可以具有默认值或隐式等。
函数对象只是函数的实例特征 (
Function1
,<一href="http://www.scala-lang.org/api/current/index.html#scala.Function2" rel="nofollow noreferrer">Function2
, ... )。当调用函数对象上的apply
方法时,将计算该函数。有特殊的语法用于定义未命名的“匿名”函数(又名“函数文字”)。函数只是一个值,因此可以被命名(例如,val f: (Int => Int) = (x => x))。类型A => B
是Function1[A, B]
的简写。在链接的SO问题中,提到了一些参考文献(例如Scala 规范)不准确地使用“函数”一词来表示“方法”或“函数对象”。我猜部分原因是方法可以根据上下文自动转换为函数对象。但请注意,相反的转换没有意义:方法不是以自己独立的方式存在于堆上的一流值。相反,方法与定义它的类有着千丝万缕的联系。
At the language level, there are only two concepts,
Methods are fundamental building blocks of Scala. Methods are always named. Methods live in classes or traits. Methods are a construct native to the JVM, and thus are the same in both Scala and Java. Methods in Scala (unlike functions) may have special features: they can be abstracted over type parameters, their arguments can have default values or be implicit, etc.
Function objects are just instances of a function trait (
Function1
,Function2
, ...). The function is evaluated when theapply
method on the function object is called. There is special syntax for defining unnamed "anonymous" functions (aka, "function literals"). A function is just a value, and as such can be named (e.g.,val f: (Int => Int) = (x => x)
). The typeA => B
is shorthand forFunction1[A, B]
.In the linked SO question, it was mentioned that some references (like the Scala spec) use the word "function" imprecisely to mean either "method" or "function object". I guess part of the reason is that methods can be automatically converted to function objects, depending on the context. Note, however, that the opposite conversion wouldn't make sense: a method is not a first-class value that lives on the heap with its own independent existence. Rather, a method is inextricably linked to the class in which it is defined.
链接问题的答案很好地涵盖了这一点,但要解决您的特定查询:
def
关键字定义的名为 method => 的 同样,所有方法的名称都方法和函数之间的区别有点像 Java 中的
int
原语和装箱的Integer
之间的区别。在一般讨论中,经常听到两者都被描述为“整数”。这通常不是问题,但在任何相关的区别处都必须注意精确。
同样,当程序需要时,方法将自动转换为函数(因此也是对象),就像装箱原语一样。因此,将方法称为函数并不是完全错误的。
更新
那么它是如何工作的呢?
当您尝试将方法作为参数传递给
List[A].map
时,编译器将生成派生Function1[A,B] 的内部类(具有合成名称)
,以及一个委托给您最初提供的方法的apply
方法。然后,此实例将作为实际参数传递。The answers to the linked question cover this fairly well, but to address your specific queries:
def
keywordThe difference between a method and a Function is somewhat like the difference between an
int
primitive and a boxedInteger
in Java.In general discussion, it's common to hear both described as being "integers". This normally isn't a problem, but you must take care to be precise wherever the distinction is relevant.
Likewise, a method will be automatically converted to a Function (and therefore an object) when your program demands it, much like boxing a primitive. So it's not entirely wrong to refer to a method as being a function.
UPDATE
So how does it work?
When you attempt to pass a method as the argument to e.g.
List[A].map
, the compiler will generate an inner class (with a synthetic name) that derivesFunction1[A,B]
, and anapply
method that delegates to the method you originally supplied. An instance of this will then be passed as the actual argument.