Contents | Prev | Next | Index


With Statements

The purpose of a with statement is to execute a statement sequence depending on the result of a type test. The with statement applies a type guard to every occurence of the tested variable within this statement sequence. Notice that within this statement sequence the qualification of record field identifiers may not be omitted. The Oberon-2 with-statement is different from the one in Pascal or Modula-2. It is more like a global guard statement.

guard_stmt       ::= WITH guard_expr first_guard
                     elsif_guard_list else_guard END
first_guard      ::= DO stmt_seq
guard_expr       ::= qualident : qualident
elsif_guard_list ::= elsif_guard_list elsif_guard
                 ::= <empty>
elsif_guard      ::= | guard_expr DO stmt_seq
else_guard       ::= ELSE stmt_seq
                 ::= <empty>

If v is a variable parameter of record type or a pointer variable, and if it is of a static type T0, the statement

WITH v:T1 DO S1
| v:T2 DO S2
ELSE S3
END

has the following meaning: if the dynamic type of v is T1 then execute statement sequence S1, where v is regarded as if it had the static type T1; else if the dynamic type of v is T2, then execute statement sequence S2, where v is regarded as if it had the static type T2; else S3 is executed. T1 and T2 must be extensions of T0. If no type test is satisfied and if there is no else clause for the with statement, the program is aborted.

Example:

WITH t:CenterTree DO i:=t.width; c:=t.subnode END


Contents | Prev | Next | Index

Canterbury Oberon-2 for JVM  (Last documentation update Jun 2, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com