Previous section: Module Variables

Dylan reference manual -- Lexical Variables

Lexical Variables

Lexical variables are created locally and can be referenced only in a limited range of program text. They correspond to local variables in other languages.
let bindings   =  init	[Special Form]
let creates new lexical variables within the smallest enclosing implicit body containing the let.

bindings are the variable(s) to be created, and may have the following forms:

variable

or ( { variable }* [ #rest rest-variable-name ] )

where variable may be either variable-name or variable-name :: type. The variable-names and rest-variable-name must have the syntax of variable names, and are not evaluated. The values returned by init provide the initial values for the variable(s) specified by bindings.

In the simplest case, bindings is just a variable name, and init returns one value which is used to initialize that variable. See Checking Types and Binding Multiple Values for more complex cases.

? begin
    let foo = 35;
    foo + foo
  end
70

A lexical variable shadows any module variable with the same name and any surrounding lexical variable with the same name. This rule means that the innermost version of a variable is the one referenced.
? define variable foo = 10;
foo
? foo
10
? begin 
    let foo = 20;
    let foo = 50;
    foo + foo
  end
100
? foo
10
The bindings introduced by the let are in effect until the end of the smallest enclosing implicit body containing the let.

Several other syntax forms, including local and block, also create lexical variables as part of their operation.

Method parameters are another example of lexical variables; they can be referenced only from the body of the method. See the chapter on Functions for more information on method parameters.

Next section: Checking Types