cast

operator

Syntax
record-type-name cast record-expression
    


Purpose

You can use cast to force a record of an extended type that is resident on a shelf of its base type to be treated as the specified type rather than the base type. Operator cast cannot be applied to non-record values. If the type of the record-expression is not a subtype of the specified record-type-name, an run-time error is thrown. For this reason, the do select-type scope should be used in preference to cast if there is a possibility of failure.

One example of when this is useful is when you get a result of a function call and then want to access fields in the record returned by that function. You know what type the result is, based on the type you passed to it, but OmniMark still treats it as a record of the base type. Here is an example:

  declare record point
     field integer x
     field integer y
   
  declare record pixel extends point
     field stream color
   
  define dynamic point function
     clone value point p
  as
     local point c
     set c:x to p:x
     set c:y to p:y
     return c
   
  define overriding pixel function
     clone value pixel p
  as
     local pixel c
     set c:x to p:x
     set c:y to p:y
     set c:color to p:color
     return c
  
  define point function
     move-point value point a
             to value point b
   as
     local point c
     set c to clone a
     set c:x to b:x
     set c:y to b:y
     return c
  
  process
     local pixel a
     local point b
    
     set a:x to 5
     set a:y to 78
     set a:color to "red"
     
     set b:x to 87
     set b:y to 56
     
     output (pixel cast move-point a to b):color || "%n"