# Basic Declarations and Definitions

A declaration introduces names and assigns them types. It can form part of a class definition or of a refinement in a compound type.

A definition introduces names that denote terms or types. It can form part of an object or class definition or it can be local to a block. Both declarations and definitions produce bindings that associate type names with type definitions or bounds, and that associate term names with types.

The scope of a name introduced by a declaration or definition is the whole statement sequence containing the binding. However, there is a restriction on forward references in blocks: In a statement sequence $s_1 \ldots s_n$ making up a block, if a simple name in $s_i$ refers to an entity defined by $s_j$ where $j \geq i$, then for all $s_k$ between and including $s_i$ and $s_j$,

• $s_k$ cannot be a variable definition.
• If $s_k$ is a value definition, it must be lazy.

## Value Declarations and Definitions

A value declaration val $x$: $T$ introduces $x$ as a name of a value of type $T$.

A value definition val $x$: $T$ = $e$ defines $x$ as a name of the value that results from the evaluation of $e$. If the value definition is not recursive, the type $T$ may be omitted, in which case the packed type of expression $e$ is assumed. If a type $T$ is given, then $e$ is expected to conform to it.

Evaluation of the value definition implies evaluation of its right-hand side $e$, unless it has the modifier lazy. The effect of the value definition is to bind $x$ to the value of $e$ converted to type $T$. A lazy value definition evaluates its right hand side $e$ the first time the value is accessed.

A constant value definition is of the form

where e is a constant expression. The final modifier must be present and no type annotation may be given. References to the constant value x are themselves treated as constant expressions; in the generated code they are replaced by the definition's right-hand side e.

Value definitions can alternatively have a pattern as left-hand side. If $p$ is some pattern other than a simple name or a name followed by a colon and a type, then the value definition val $p$ = $e$ is expanded as follows:

1. If the pattern $p$ has bound variables $x_1 , \ldots , x_n$, where $n > 1$:

###### Example

Consider the object definition:

Then the block

is equivalent to the block