Using an as Clause in foreach Instructions
The foreach instruction optional as clause type expression can be used to relax compile time restrictions in the target variable of a foreach instruction. Without an as clause present, the type of the target variable must be the same as or a super-type of the member type of the collection. For examples of use of the as clause, see "Examples of the as Clause Expression", in the following subsection.
Explicit Type Name
If an as clause type expression is present, it can specify an explicit type name that is a sub-type of the membership type of the collection.
The Jade compiler checks that the as clause type is a sub-type of the membership type of the collection, and raises exception 6460 (Type is not a sub-type of collection membership) if it is not.
If an as clause type is present, the Jade compiler allows the target variable type of the foreach instruction to be any valid super-type of the as clause type, rather than the collection’s membership type. If the type of the target variable is not compatible, exception 6072 (Incompatible types) is raised.
Type Expression
An as clause does not need to be an explicit type name. It can be any variable or valid expression that yields a type or subclass of Type except for PseudoType.
As an expression means the as clause type cannot be evaluated until run time, the compile-time target variable validation is omitted and occurs as a runtime check.
Runtime Validation
In addition to the compile-time validation, if an as clause is present, additional runtime validation occurs, as follows.
-
Before any foreach instruction iteration takes place, the type of the as clause is checked against the evaluated membership type of the collection to make sure it can actually contain values of the specified as clause type; that is, the membership type of the collection and the type of the as clause must be related.
If the evaluated types are not compatible, exception 1022 (Type is incompatible with foreach collection membership) is raised. If this exception is resumed, the foreach instruction is skipped.
For primitive type collections, the membership type of the collection and the type of the as clause must be the same; otherwise exception 1022 is raised.
-
If a non‑explicit type name as clause is present, before any foreach instruction iteration takes place, the type of the as clause is checked against the type of the target variable, to make sure values returned can be assigned to the target variable.
If it is not assignment‑compatible, exception 1023 (Type is incompatible with foreach target variable) is raised. If this exception is resumed, the foreach instruction is skipped.
-
At the beginning of each iteration, a compatibility check takes place between the type of the returned iteration value and the type of the as clause.
If the type of the retrieved value is not the same as or a sub-type of the type of the as clause, exception 1470 (Retrieved value type is incompatible with as clause type) is raised. If this exception is resumed, the foreach instruction will not enter the foreach but rather retrieve the next object from the iterator.
2020.0.01 and higher