Closures in Scala
Scala Tutorial
Let’s First Understand Free Variables
What is Free Variable?
The concept of free variable comes from mathematics. In computer programming, the term free variable refers to variables used in a function that are neither local variables (defined in the function’s body)nor parameters of that function. As name suggest, it’s a free variable because we don’t put any limitations on it — it can represent almost any value, for instance, it can be 1 or -1, or any value.
How it Works?
When the interpreter/compiler evaluates a program, it needs to know the values of variables — if it can’t find a binding of any variable, it take further actions as follows:
- It either reports an error and stops evaluating the program further.
- Or continues to look up that particular variable in the parent or immediate outer scope (see if the variable is defined somewhere outside the current function).
What is Closure and How it Relates to Free Variables?
First off, a closure is an important concept in the world of functional programming. It is also known as lexical closure or function closure. It’s a “first-class function” whose return value depends on the value of one or more “free variable” defined outside this function. A closure may be a pure or impure function. Also, it may be named or anonymous function.
State of Free Variables
Note that when a closure gets executed, it takes the most recent state (value) of the free variable. If closure modifies (in case of mutable type) the free variable, the changes are visible outside the closure — it’s like changing the state of global variable.
Normal Function vs. Closure
The difference between a normal function and a closure is that a closure dependents on one or more free variables.
Motivation (What’s the Need)
In certain use cases, objective-oriented programming is more flexible than functional programming, because, unlike functions, objects carry two things:
- Operations (Methods)
- State (Data or Variable)
It is important to note that unlike objects, functions doesn’t have any state — it just performs operations only.
To mitigate the problem of not having the state in functions, closure and free variables work hand in hand making it possible to pass a bunch of data elements or states along with a function.
Closures in Scala
Note that in Scala, closures are not widely used as the language itself is not a pure functional programming — rather, it’s hybrid language having both object oriented programming and functional programming.
object ClosureDemo extends App {
val factor = 3 // free variable
val multipler = (i: Int) => i * factor println(multipler(30))
}
In the above example, i and factor are the two free variables — i is the formal parameter to the function; factor is free variable (not a formal parameter) and has a reference to a variable outside the function but in the enclosing scope.
Closure Being a Pure Function
In order for any closure being a pure function, the free variables must be defined as “val” (or immutable) instead of “var”. It’s a good practice to keep our free variables as immutable so that the closure is referentially transparent.
In Case of “var”
If we use “var” to maintain the state by persisting the value over several invocations of the function, then the closure is no longer having referential transparency and thus no longer a pure function.