define conversion-function

declaration/definition

Syntax
define type conversion-function
 value type input-value
 (as function-body | elsewhere)



Purpose

You can define a conversion function that will be called automatically when data of one type is expected and data of another type is supplied. The following example define a conversion function that converts an integer to a stream:

  define string conversion-function
   value integer x
   as
      return "d" % x
  
  process
     local integer foo initial {5}
     output foo

A conversion function is not called explicitly. It is called automatically when needed. Therefore, a conversion function has no name. It can be referred to by the type of conversion it supports, so the above function can be called an "integer to stream" conversion function.

You can define a conversion function for:

  • Any of OmniMark's built in types
  • Any type defined as an opaque data type by an external function library
  • Any type defined as an OmniMark record type

OmniMark supplies conversion function for most of its internal types, and the external function libraries supplied with OmniMark define conversion functions between the OmniMark types and the types they create. There can only be one conversion function for any one type-to-type conversion so you cannot define conversion-functions for conversions that are already supported.

When defining conversion functions, it is sometimes useful to take advantage of the type conversions that already exist. The following conversion functions define conversions between the bcd and floatdata types using the string representation of the number as an intermediate type. This takes advantage of the fact that the BCD and float libraries both define string formatting and conversions from strings for their respective data types:

  define bcd conversion-function value float f
   as
     return "d" % f
  
  define float conversion-function value bcd b
   as
     return "d" % b

Conversion functions and extended records

There are two limitations when defining conversion functions that deal with extended records:

  • Only one conversion function with the same result type can be defined whose argument is a type in any given line of extension. That is, if B extends A and C extends B, then only one conversion function with the same result type can be defined whose argument is of type A, B, or C. You cannot define conversion functions for all three. (However, you can define a dynamic function for each member of the extended set and then call that dynamic function from a conversion function for the base type of the set.)
  • Only one conversion function, from a given type, can be defined whose return type is any member of an extended family of types. That is, if B extends A, and C also extends A, then only one conversion function (from the same argument type) can be defined whose return type is A, B, or C. You cannot define conversion functions for all three.