What is a named function in scala?

I am confused about the "named function".

We define the method with def, and the method name appears after def.

For example, def m() = () => ()we can say the name of this method m. The "named method" is easy to understand, and I'm sure there is no anonymous method.

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

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

I am trying to give an example myself here. For example, val f = () => ()I believe the name of this function f. So there is a named function here.

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

Does the concept of a named function make sense? I am completely spoiled.

+4
source share
2 answers

I think that “named function” refers, for example, to nested functions defined using a keyword def. Consider the following example where it matters:

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

println(foo) // returns 58

The part x => return xhere is the body of the abnormal function literal. The fact that a function literal is assigned to a variable with a name fdoes not affect the operator return: when it is reached return, the whole method is returned foo. In this case, he returns 58, and 42will never be reached.

:

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

println(bar) // returns 42

return f, bar. f(58) 58 . bar 42.

"" , "" . , f - , , bar, , bar, "", "".

+1

" "?

:

return e , .

, . , @som-snytt, :

object A extends Function1[Int, Int]

.

:

Lightbend, "named method or function" :

- "". , "" "". return, scalprod "". Ill PR, .

, .

+1

Source: https://habr.com/ru/post/1696060/


All Articles