Sometimes I get away from writing Scala for a while, and when I come back to it I see a piece of code that looks like this following example and I wonder, “What is Foo
, and how does this code work?”:
val f1 = Foo {
println("hello from the `f1` instance")
"this is the result of the block of code"
}
Well, one way it can work is that Foo
can be a class that accepts a function parameter, and so everything inside those curly braces is the argument to Foo
. For example, this code shows how a Foo
class can be defined to accept a function as a constructor parameter:
case class Foo[A, B](f: A => B) {
// more code here ...
}
That code says that Foo
is a Scala case class that takes one constructor parameter, which is a function that transforms a generic type A
into a resulting generic type B
.
Going back to the first code I showed, everything inside the curly braces is that first argument to Foo
:

Informally speaking, everything inside a set of curly braces in Scala can be thought of as “a block of code,” and typically that block of code has some return value, such as "hello"
in this example.