This is actually a bit complicated. First, let's see what happens outside the REPL:
Doesn't work when funcis a local variable:
object Main extends App {
def foo() = {
val f = (_: Int) + 1
f _
}
println(foo())
}
[error] /tmp/rendereraEZGWf9f1Q/src/main/scala/test.scala:8: _ must follow method; cannot follow Int => Int
[error] f _
[error] ^
But if you put it out def foo, it will compile:
object Main extends App {
val f = (_: Int) + 1
val f1 = f _
println(f1)
}
because it fis both a field Mainand a method with no arguments, which returns the value of this field.
, REPL ( Scala trait/class/object),
scala> val func = (x: Int) => x
func: Int => Int = <function1>
-
object Line1 {
val func = (x: Int) => x
}
import Line1._
// print result
, func Line1.func, .