LilyPond Contributor's Guide: 10.14 LilyPond scoping

## 10.14 LilyPond scoping

The LilyPond language has a concept of scoping, i.e., you can do:

>     foo = 1

>     #(begin
>        (display (+ foo 2)))

with `\paper`, `\midi` and `\header` being nested scope inside the ‘`.ly`’ file-level scope. `foo = 1` is translated in to a scheme variable definition.

This implemented using modules, with each scope being an anonymous module that imports its enclosing scope’s module.

LilyPond’s core, loaded from ‘`.scm`’ files, is usually placed in the `lily` module, outside the ‘`.ly`’ level. In the case of

>     lilypond a.ly b.ly

we want to reuse the built-in definitions, without changes effected in user-level ‘`a.ly`’ leaking into the processing of ‘`b.ly`’.

The user-accessible definition commands have to take care to avoid memory leaks that could occur when running multiple files. All information belonging to user-defined commands and markups is stored in a manner that allows it to be garbage-collected when the module is dispersed, either by being stored module-locally, or in weak hash tables.

#### LilyPond — Contributor’s Guide v2.24.4 (stable-branch).