3

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?

Community
  • 1
  • 1
Ying
  • 2,660
  • 24
  • 23

2 Answers2

7

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 the apply 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 type A => B is shorthand for Function1[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.

Community
  • 1
  • 1
Kipton Barros
  • 21,002
  • 4
  • 67
  • 80
  • The function->method conversion is unavoidable, with the method in question being `apply` as defined on the function class. Very much like my primitive boxing analogy :) – Kevin Wright Sep 08 '11 at 11:20
  • I would say it differently: every function *represents* an `apply` method defined on an instance of a function class. There's no conversion here, that's just how functions are defined. On the other hand, converting a method into a function actually does allocate and build something new. – Kipton Barros Sep 08 '11 at 15:20
  • I asked this question because the mixing of the method and function terms confused me -- is a method a function? or a named function? or not a function at all? this answer used the last definition, i.e., a method is not a function. This definition is not true from the Scala language specification (see my question above). That's something I tried to clarify. – Ying Sep 09 '11 at 21:33
  • You are right: "function" is used in a non-technical sense as a generic term for *methods* as well as *function objects*. Updated the answer to clarify this. – Kipton Barros Sep 10 '11 at 05:54
7

The answers to the linked question cover this fairly well, but to address your specific queries:

  • method => The thing you define with the def keyword
  • named method => The same, all methods have names
  • named function => a function that has been assigned to a value, or converted from a method. As contrasted with an anonymous function.

The difference between a method and a Function is somewhat like the difference between an int primitive and a boxed Integer 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 derives Function1[A,B], and an apply method that delegates to the method you originally supplied. An instance of this will then be passed as the actual argument.

Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
  • The boxing comparison helps. I understood that Scala needs to transform function literal into a method of FunciontN because Scala is a an OO language. But why does Scala "box" a method into another method if you provide map a regular object method? – Ying Sep 09 '11 at 21:25
  • A method will never be "boxed" into another method, only into a function. When you specify a method as an argument to e.g. `List[A].map`, it's actually being passed as a function `A => B`. Internally, the map method will then call `apply` on the supplied function instance which will, in turn, call the method you specified. – Kevin Wright Sep 10 '11 at 12:46