Q:

How to execute a function at the time of its definition in Scala

Is there a more idiomatic way to execute a function upon its definition in Scala? Can I do better than the following Function1 definition with its immediate execution?
scala> (new Function1[Int, Int] { def apply(v1: Int) = v1 }) (5)
res0: Int = 5

Are there use cases that justify such calls? I have none, but got curious after I'd been asked about it and thought it might be helpful to find out.

A:

Scala does have a terse syntax for anonymous functions (which you can call immediately):
((x: Int, y: Int) => x + y)(4, 5)

The only use case I am aware of for such instant calls is from another language - JavaScript - where they are heavily used for scoping. But in Scala, you can just define scopes with curly brackets, like:
{
    val a = 5
}
{
    val b = 4
    // a not accessible here
}

so I don't see a use case where such a construct would be useful in Scala.

