10.7 Retyping or Casting


In some circumstances, it is desirable to use a feature that is defined on a subtype of the type that is expected at that point in the expression. Because the feature is not defined on the expected type, this results in a type conformance error.

When you are certain that the actual type of the object is the subtype, the object can be retyped using the operation oclAsType , which takes as parameter a type name . This operation results in the same object, but the expected type within the expression is the indicated parameter. When you have an object object of type Type1, and Type2 is another type, it is allowable to write

 object.oclAsType(Type2) --- evaluates to object with type Type2 

An object can only be re-typed to one of its subtypes ; therefore, in the example, Type2 must be a subtype of Type1 .

If the actual type of the object is not a subtype of the type to which it is re-typed, the result of the expression is undefined (see Section 10.6).



Object Constraint Language, The. Getting Your Models Ready for MDA
The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)
ISBN: 0321179366
EAN: 2147483647
Year: 2003
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net