Lexical scope vs. dynamic scope

One of the basic concepts of all programming languages is scoping of variables. There existing two main concepts: lexical scope and dynamic scope.

 
Lexical scope

The following example shows an F# function. The function foo uses the variable x to calculate a result. The value x will be defined twice with different content. The first value for x will be set where the function foo is created and then x will be changed where the function foo is called.

let example1 =
    let x = 1
    let foo y = x + y   
    let x = 2
    foo 5

If you execute the example, it will returns 6, because F# uses lexical scope. Whenever a function is called it uses the scope (the environment) where the function was created. In this example we have two scopes. One where the function is created and one where the function is called. In the first scope the variable x is 1 and in the second scope x is 2. Therefore the first scope with x = 1 is used and the example function returns 6.

 
Let’s validate this behaviour by creating a second example.

let example2 =
    let x = 1
    let foo y = x + y   
    let x = 2
    let bar y = x + y   
    foo 5 + bar 5

This example function returns 13 because foo is executed in the first scope with x = 1 and bar is executed in the second scope with x = 2.

 
But why do I have created examples which uses a function which executed other functions? Wouldn’t it be easier to use the following source code?

let x = 1
let foo y = x + y   
let x = 2
foo 5

 

Yes, of course, this example is easier but it has a big disadvantage: it will result in a compiler error. The F# compiler show the following error in line 3: Duplicate definition of value ‘x’. This is because the main scope in an F# application is always a single scope. Therefore it is not possible to create a second scope and the compiler will not allow the redefinition of a variable.

 
Dynamic scope

In contrast to lexical scope, the dynamic scope always uses the environment where a function is called. Let’s use the first example to explain the behaviour of a function in dynamic scope.

let example1 =
    let x = 1
    let foo y = x + y   
    let x = 2
    foo 5

 
If we execute this function in a language which uses dynamic scope it will return 7 because the actual scope will be used where the function is called. In this scope x is 2 and therefore the result is 7.

But the dynamic scope behaviour has some major disadvantages. For example the readability of your source code will decrease and it would be very difficult to find errors. Within this short example you don’t have these disadvantages, but what if the function foo is created somewhere else in the application? In this case you must know that a variable x is used in this function. Otherwise you cannot use this function correctly.  Or you even create errors because you create a variable x which you want to use in your scope but you have this side effect which changes the function behaviour because the new value of x is also used in the function call.

 
Summary
Lexical scope means to use the environment where a function was created. Dynamic scope means to use the environment where the function is called. In most of the modern programming languages only lexical scope is supported. But dynamic scope is sometimes also helpful and will be used for example in macro languages.

Advertisements
Dieser Beitrag wurde unter .NET, F# abgelegt und mit , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s