[NEXT] [PREV]    HANDBOOK / GENTLE PRIMER / Control Structures /



In our example, the variables A and B are defined outside the alternative statement. They can be used inside.

The general rule is: A variable that is used in a pattern must be in a defined state at the beginning of the whole alternative statement, or must defined by a preceding member of the same alternative.

In the example, the variable C is used after the alternative statement, and has no definition outside the alternative statement. This is allowed because it is defined in both alternatives. Regardless of which alternative is taken, C would be defined in all cases (if no alternative is applicable then statement using C will not be reached).

The general rule is: A variable that is defined in all alternatives is said to be defined by the whole alternative statement, and may be used subsequently. If a variable is defined in all alternatives, it must be defined everywhere with the same type.

If a variable is defined in one or more - but not all - alternatives, it is said to be local to the alternative containing the definition and cannot be used outside the alternative statement.

If, in the above example, we had written

   (| ge(A, B) process(A -> ResultA) || process(B -> ResultB) |)
neither ResultA nor ResultB could have been used in a subsequent statement because it could not be guaranteed that it is always defined.