# Section 8.1.2.5:
CAST

###### class CAST{T}

#### Formal Types

The type definitions in this class specification fall into two groups :-

1. The definition of CAST{T} itself (including SAME).
2. The auxiliary vdm type needed to define a Sather type argument used in the feature definitions which follow.

types
SAME = CAST_T ;
-- Auxiliary Types
Any_Type = @T

This class is provided to permit explicit narrowing from the general abstract class $OB to the argument class T. It may then be used as a substitute for the 'one line' typecase statement.

#### create

This creation operation serves merely to encapsulate the class argument type. There is no internal state other than this.

create |

##### Formal Signature

create() res : SAME

##### Post-condition

Since there are no arguments the post-condition is vacuously true.

This creation operation merely encapsulates the class argument type for use when casting objects.

#### from

This is the single 'working' feature of this class, permitting an in-line typecase to be written. Note that if the argument happens not to be of type T then an exception will be raised.

from | (
| anything : $OB
| ) : T

##### Formal Signature

from(self : SAME, anything : $OB) res : SAME

##### Pre-condition

Since the argument can be of any type then this is vacuously true!

##### Post-condition

Note that this post-condition is always valid, since otherwise an exception has been raised.

post typeof(res) = Any_Type

##### Exception condition

For the meaning of the raise statement see its specification in section 6.7.12. The specification of the typeof auxiliary function is given in Section 6.9.

errs Exception : typeof(anything) <> Any_Type -> raise anything

This routine allows an explicit typecase 'cast' to be made to an object of the class argument type. An exception is raised if anything is not of class T.

| Language Index |  | Library Index |  | Basic Index

Comments or enquiries should be made to Keith Hopper. Page last modified: Thursday, 16 November 2000. |