Scala Closures
In Scala, a closure is a function that can capture and contain free variables from its defining environment.
A closure is a function whose return value depends on one or more variables declared outside the function.
A closure can access variables outside the function and use them inside the function; even when a variable's scope has exceeded its defining environment, the closure can still access it.
The key to a closure is that it can "close over" variables outside the function, making them still available inside the function.
Such as the following anonymous function:
val multiplier = (i:Int) => i * 10
The function body contains a variable i, which serves as a parameter of the function. Consider another piece of code below:
val multiplier = (i:Int) => i * factor
In multiplier there are two variables: i and factor. One of them, i, is a formal parameter of the function; when the multiplier function is called, i is assigned a new value. However, factor is not a formal parameter but a free variable. Consider the following code:
var factor = 3
val multiplier = (i:Int) => i * factor
Here we introduce a free variable factor, which is defined outside the function.
The function variable multiplier defined this way becomes a "closure" because it references a variable defined outside the function; the process of defining this function captures the free variable to form a closed function.
Complete example
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
println
(
"muliplier(1) value = "
- multiplier
(
1
)
)
println
(
"muliplier(2) value = "
- multiplier
(
2
)
)
}
var
factor
=
3
val
multiplier
=
(
i
:
Int
)
=>
i
factor
}
Run Example »
The output of the code above is:
$ scalac Test.scala
$ scala Test
muliplier(1) value = 3
muliplier(2) value = 6
In the following example, the closure function captures the external variable externalVar; when the value of externalVar changes, the result computed inside the closure changes accordingly.
Example
object
ClosureExample1
{
def
main
(
args
:
Array
[
String
]
)
:
Unit
=
{
var
externalVar
=
10
val
closure
=
(
x
:
Int
)
=>
x + externalVar
println
(
closure
(
5
)
)
// Output 15
externalVar
=
20
println
(
closure
(
5
)
)
// outputs 25
}
}
The output of the code above is:
15
25
Closures as Return Values
In the following example, the makeAdder function returns a closure. Different closure instances capture different adder values.
Example
object
ClosureExample2
{
def
main
(
args
:
Array
[
String
]
)
:
Unit
=
{
def
makeAdder
(
adder
:
Int
)
:
Int
=>
Int
=
{
(
x
:
Int
)
=>
x + adder
}
val
addFive
=
makeAdder
(
5
)
val
addTen
=
makeAdder
(
10
)
println
(
addFive
(
3
)
)
// Output 8
println
(
addTen
(
3
)
)
// outputs 13
}
}
The output of the code above is:
8
13
Types of Variables Captured by Closures
Closures can capture variables of different types, including value types and reference types.
In the following example, the addMessage closure captures the messages variable and can modify it.
Example
object
ClosureExample3
{
def
main
(
args
:
Array
[
String
]
)
:
Unit
=
{
var
messages
=
List
(
"Hello"
,
"World"
)
val
addMessage
=
(
msg
:
String
)
=>
messages
=
messages
:
- msg
addMessage
(
"Scala"
)
println
(
messages
)
// outputs List(Hello, World, Scala)
}
}
The output of the code above is:
List(Hello, World, Scala)
Closures and Functional Programming
Closures are an important concept in functional programming, widely used in techniques such as higher-order functions and currying.
In the following example, the highOrderFunction function takes a function as a parameter. The closure passed in captures the external variable externalVar.
Example
object
ClosureExample4
{
def
main
(
args
:
Array
[
String
]
)
:
Unit
=
{
def
highOrderFunction
(
f
:
Int
=>
Int, x
:
Int
)
:
Int
=
f
(
x
)
val
externalVar
=
5
val
closure
=
(
x
:
Int
)
=>
x + externalVar
println
(
highOrderFunction
(
closure,
10
)
)
// Output 15
}
}
The output of the code above is:
15