[Next] [Previous] [Up] [Top] [Contents] [Index]

2.5 Statements and Expressions

2.5.2 Variable Declarations

Variable declarations have the following syntax:

let_decl	::=	"let" ["var"] name {pragma} ":=" expr ";"

If the var annotation is used, the variable may be assigned a new value using an assignment statement. Otherwise, the variable binding is constant. (The contents of the variable may still be mutable.) Formal parameters are treated as constant variable bindings and so are not assignable. The initializing expression is evaluated in a context where the name of the variable being declared and any variables declared later within the same declaration block are considered undefined. This avoids potential misunderstandings about the meaning of apparently self-referential or mutually recursive initializers while still supporting a kind of let* [Steele 84] variable binding sequence.

Variable declarations may appear at the top level as well as inside a method. However, the ordering of variable declarations at the top level (and consequently the order of evaluation of the initializing expressions) is less obvious. In the current UW Cecil implementation, the textual ordering of variable declarations is used to define an ordering for evaluating variable initializers. (Similarly, statements interspersed with top-level declarations are evaluated in the order given.) We would prefer a semantics that was independent of the "order" of variable declarations at the top level, so that all top-level declarations are considered unordered. Possible alternative semantics which have this property include restricting variable initialization expressions to be simple expressions without side-effects (thereby making the issue of evaluation order unimportant), eliminating variable declarations at the top level entirely, or supporting a form of on-demand at-most-once evaluation of top-level variable initializers akin to the lazy evaluation semantics of field initializers (see section 2.3.4).