scala学习笔记:函数与方法
来源:互联网 发布:bf109战斗机数据 编辑:程序博客网 时间:2024/05/18 20:09
http://stackoverflow.com/questions/2529184/difference-between-method-and-function-in-scala
A Function Type is (roughly) a type of the form (T1, ..., Tn) => U, which is a shorthand for the trait FunctionN
in the standard library. Anonymous Functions and Method Values have function types, and function types can be used as part of value, variable and function declarations and definitions. In fact, it can be part of a method type.
A Method Type is a non-value type. That means there is no value - no object, no instance - with a method type. As mentioned above, a Method Value actually has a Function Type. A method type is a def
declaration - everything about a def
except its body.
Value Declarations and Definitions and Variable Declarations and Definitions are val
and var
declarations, including both type and value - which can be, respectively, Function Type andAnonymous Functions or Method Values. Note that, on the JVM, these (method values) are implemented with what Java calls "methods".
A Function Declaration is a def
declaration, including type and body. The type part is the Method Type, and the body is an expression or a block. This is also implemented on the JVM with what Java calls "methods".
Finally, an Anonymous Function is an instance of a Function Type (ie, an instance of the trait FunctionN
), and a Method Value is the same thing! The distinction is that a Method Value is created from methods, either by postfixing an underscore (m _
is a method value corresponding to the "function declaration" (def
) m
), or by a process called eta-expansion, which is like an automatic cast from method to function.
That is what the specs say, so let me put this up-front: we do not use that terminology! It leads to too much confusion between so-called "function declaration", which is a part of the program (chapter 4 -- basic declarations) and "anonymous function", which is an expression, and "function type", which is, well a type -- a trait.
The terminology below, and used by experienced Scala programmers, makes one change from the terminology of the specification: instead of saying function declaration, we say method. Or even method declaration. Furthermore, we note that value declarations and variable declarations are also methods for practical purposes.
So, given the above change in terminology, here's a practical explanation of the distinction.
A function is an object that includes one of the FunctionX
traits, such as Function0
, Function1
, Function2
, etc. It might be including PartialFunction
as well, which actually extends Function1
.
Let's see the type signature for one of these traits:
trait Function2[-T1, -T2, +R] extends AnyRef
This trait has one abstract method (it has a few concrete methods as well):
def apply(v1: T1, v2: T2): R
And that tell us all that there is to know about it. A function has an apply
method which receives Nparameters of types T1, T2, ..., TN, and returns something of type R
. It is contra-variant on the parameters it receives, and co-variant on the result.
That variance means that a Function1[Seq[T], String]
is a subtype of Function1[List[T], AnyRef]
. Being a subtype means it can be used in place of it. One can easily see that if I'm going to call f(List(1, 2, 3))
and expect an AnyRef
back, either of the two types above would work.
Now, what is the similarity of a method and a function? Well, if f
is a function and m
is a method local to the scope, then both can be called like this:
val o1 = f(List(1, 2, 3))val o2 = m(List(1, 2, 3))
These calls are actually different, because the first one is just a syntactic sugar. Scala expands it to:
val o1 = f.apply(List(1, 2, 3))
Which, of course, is a method call on object f
. Functions also have other syntactic sugars to its advantage: function literals (two of them, actually) and (T1, T2) => R
type signatures. For example:
val f = (l: List[Int]) => l mkString ""val g: (AnyVal) => String = { case i: Int => "Int" case d: Double => "Double" case o => "Other"}
Another similarity between a method and a function is that the former can be easily converted into the latter:
val f = m _
Scala will expand that, assuming m
type is (List[Int])AnyRef
into (Scala 2.7):
val f = new AnyRef with Function1[List[Int], AnyRef] { def apply(x$1: List[Int]) = this.m(x$1)}
On Scala 2.8, it actually uses an AbstractFunction1
class to reduce class sizes.
Notice that one can't convert the other way around -- from a function to a method.
Methods, however, have one big advantage (well, two -- they can be slightly faster): they can receive type parameters. For instance, while f
above can necessarily specify the type of List
it receives (List[Int]
in the example), m
can parameterize it:
def m[T](l: List[T]): String = l mkString ""
I think this pretty much covers everything, but I'll be happy to complement this with answers to any questions that may remain.
- scala学习笔记:函数与方法
- Scala学习笔记-方法和函数
- scala函数与方法
- scala函数与方法
- Scala学习笔记17【Scala偏函数】
- Scala 学习笔记(六)------Scala 函数嵌套
- scala学习笔记:理解函数
- scala学习笔记之函数
- scala函数使用--学习笔记
- scala学习笔记 之 函数
- scala中的方法与函数
- Scala学习笔记-apply方法
- Scala学习笔记之Scala函数及函数式编程
- Scala学习笔记03【学习识别Scala函数式风格】
- scala学习笔记三----scala函数式编程风格学习
- Scala学习笔记07【For、Scala函数进阶】
- scala中的函数、变量、集合、对象---scala学习笔记(1)
- Scala学习笔记(三)scala的函数式编程
- 记录遇到的ios下的坑和bugs
- JqueryEasyUI datagrid通过struts请求从后台读取数据
- HTTP请求(GET与POST区别)和响应
- php ajax出现中文的解决办法
- 优化SQL查询:如何写出高性能SQL语句
- scala学习笔记:函数与方法
- Android WebView
- 银行账户密码 在对银行账户等重要权限设置密码的时候......
- iOS8 UIAlertController 创建UIAlertView以及UIActionSheet
- ASP.NET管线与应用程序生命周期
- hibernate的get和load区别
- 用Python实现一个简单的WebSocket服务器
- hdu4111 Alice and Bob 博弈论
- android:ToolBar详解(手把手教程)