Skip to content
hrj edited this page Jun 24, 2016 · 4 revisions

Scopes, in abandon, are the means of limiting the effects of their contents. Every input file has an implicit scope around it. Explicit scopes can be defined by enclosing them in { and }. Scopes can be nested within each other. The include directive attaches the included file's scope into the scope where it is included.

As a simple example, consider the following ledger file:

def fx = 30
{
  def x = 10
  {
    def y = x * 10
  }
}

In the above, there are three scopes:

  • First is the file-level scope. In this scope fx is being defined. It will be visible throughout the file.
  • The second level scope is the one defined by the outermost braces { and }. In this scope x is being defined and it will be visible only in this scope and its nested scopes. It will not be visible in the file's scope.
  • The third level scope is defined by the innermost braces. In this y is being defined and accesses x in its definition.

The benefit of scopes is that they make it easier to reason about the system. The visibility and effect of variables or directives can be limited.

For example, when a default account is defined, it is a good idea to limit the scope in which it is default:

{
def defaultAccount = "Cash"
  
2013/4/1
  Expense     100         ; Cash will be used as the default account for balancing the transaction.
}

; The effect of defaultAccount will not be applicable here (outside the scope of its definition)
Clone this wiki locally