Difference between method and function in Scala
-
i read http://www.naildrivin5.com/scalatour/wiki_pages/ScalaFunctions. In that post he specified Methods and functions are not the same thing. But he didn't explain anything about it. can anyone explain what he was trying to say.
-
Answer:
Anantha Kumaran at Stack Overflow Visit the source
Other answers
Jim has got this pretty much covered in his blog, but I'm posting a briefing here for reference. First, let's see what the Scala Specification tell us. Chapter 3 (types) tell us about Function Types (3.2.9) and Method Types (3.3.1). Chapter 4 (basic declarations) speaks of Value Declaration and Definitions (4.1), Variable Declaration and Definitions (4.2) and Functions Declarations and Definitions (4.6). Chapter 6 (expressions) speaks of Anonymous Functions (6.23) and Method Values (6.7). Curiously, function values is spoken of one time on 3.2.9, and no where else. 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 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 and anonymous functions or method values. Note that, on the JVM, these 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 N parameters 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.
Daniel C. Sobral
One big practical difference between a method and a function is what return means. return only ever returns from a method. For example: scala> val f = () => { return "test" } <console>:4: error: return outside method definition val f = () => { return "test" } ^ Returning from a function defined in a method does a non-local return: scala> def f: String = { | val g = () => { return "test" } | g() | "not this" | } f: String scala> f res4: String = test Whereas returning from a local method only returns from that method. scala> def f2: String = { | def g(): String = { return "test" } | g() | "is this" | } f2: String scala> f2 res5: String = is this
Ben Lings
Functions don't support parameter defaults. Methods do. Converting from a method to a function loses parameter defaults. (Scala 2.8.1)
eptx
Scala Functions vs Methods better explained here http://agileskills2.org/blog/2011/08/21/revealing-the-scala-magician%E2%80%99s-code-method-vs-function/
Rajesh
Related Q & A:
- What is the difference between a static method and class method in Python?Best solution by pythoncentral.io
- How to access a non static method in an abstract class's static method?Best solution by Stack Overflow
- How to access a nested function from another nested function in javascript?Best solution by devarticles.com
- how to call a function in Python in another function?Best solution by Yahoo! Answers
- What is the use of function Types in scala?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.