lzwang2
lzwang2

Reputation: 95

What is a named function in scala?

I'm confused about "named function".

We define a method by def, and the name of the method comes after def.

e.g.def m() = () => () We can say the name of this method is m. "Named method" is easy to understand, and I'm sure, there is no anonymous method.

I see explanations of "anonymous function" everywhere. I know an anonymous function is an instance of a function type, and we can easily find examples of an anonymous function.

Then, a named function must also be an instance of a function type.

I try to give an example myself here. e.g. val f = () => () I suppose that the name of this function is f. And so, there is a named function here.

But when I use tool JD-GUI to decompile the class file, it seems to me that f is not a name, but a ObjectRef.

Does concept "named function" make sense? I'm totally messed up.

Upvotes: 4

Views: 808

Answers (2)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

Does concept "named function" make sense?

The specification states:

A return expression return e must occur inside the body of some enclosing named method or function.

The named part refers only to the method, not the function. Although as @som-snytt elaborates, we can have named functions as well:

object A extends Function1[Int, Int]

Would be regarded as named function.

Edit:

Contacted Lightbend, this is the reply regarding the "named method or function" part of the spec definition:

This is a spec bug — it should just say “method”. There is unfortunately some confusion in the spec between “method” and “function”. You can see an instance of this right above the passage on return expressions, where the scalprod method is called a “function”. I’ll submit a PR to clarify the section around non-local returns.

To answer your question, you should only regard method as to the scoping rules of a return expression.

Upvotes: 1

Andrey Tyukin
Andrey Tyukin

Reputation: 44918

I think "named function" refers for example to nested functions defined with the def keyword. Consider the following example where this makes a difference:

def foo: Int = {
  val f: Int => Int = x => return x
  f(58)
  42
}

println(foo) // returns 58

The x => return x part here is the body of an anonymous function literal. The fact that the function literal is assigned to the variable with name f has no influence on the return statement: when the return is reached, the whole method foo returns. In this case, it returns 58, and the 42 is never reached.

However, this is different for a named nested function:

def bar: Int = {
  def f(x: Int): Int = {
    return x
  }
  f(58)
  42
}

println(bar) // returns 42

Here, the return leaves only the nested function f, not the whole bar-method. The expression f(58) in statement position evaluates to 58 and is then ignored. The method bar then returns 42.

The word "function" makes more sense than the word "method" in this context. For example, the nested function f is not really tied to some class other than the class that encloses bar, and it cannot be invoked on instances of class that encloses the method bar, therefore it might be preferable to call it "function" rather than "method".

Upvotes: 1

Related Questions