swirl
Guide to OmniMark 9   OmniMark home
docs home 
IndexConceptsTasksSyntaxLibrariesLegacy LibrariesErrors
 
  Related Syntax   Related Concepts  
operator  

cast

 
 

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"

    Related Syntax
 
Related Concepts
 
 

Top [ INDEX ] [ CONCEPTS ] [ TASKS ] [ SYNTAX ] [ LIBRARIES ] [ LEGACY LIBRARIES ] [ ERRORS ]

OmniMark 9.1.0 Documentation Generated: September 2, 2010 at 1:51:21 pm
If you have any comments about this section of the documentation, please use this form.

Copyright © Stilo International plc, 1988-2010.