export, export as opaque

declaration/definition

Syntax
export function definition
     | function predefinition
     | global variable declaration
     | global variable predefinition
     | constant declaration
     | constant predefinition
     | catch declaration
     | (as opaque)? record declaration
     | (as opaque)? record predeclaration
or
import module name export? prefix declararion (export? use declaration)*


Purpose

You can export various properties of a module using the export keyword. Normally, the properties of a module are private to the module. In order to make properties available for use in the program that imports the module, you must explicitly export those properties. The properties that are available for export from a module are as follows:

Exporting functions

To export a function from a module, replace the word define with the word export:

  export string source function timestamp
   as output date "=xY-=M-=D =H:=m:=s"

You can export a function predefinition (using elsewhere) but if you do so, you must also export the function definition. It is an error if the export status of the predefinition and the definition do not match.

Exporting global variables

To export a global variable from a module, precede the word global with the word export:

  export global stream foo

You can export a global predefinition (using elsewhere) but if you do so, you must also export the global definition. It is an error if the export status of the predefinition and the definition do not match.

Exporting constants

To export a constant from a module, precede the word constant with the word export:

  export constant stream greeting initial {"Hello"} 

You can export a constant predefinition (using elsewhere) but if you do so, you must also export the constant definition. It is an error if the export status of the predefinition and the definition do not match.

Exporting catch labels

To export a catch label from a module, replace the word declare with the word export:

  export catch close-parenthesis

You will need to export catch names from a module if there is any chance of having a catch and a throw of the same name on opposite sides of the module boundary (that is, one inside the module, and one outside it).

Exporting record type declarations

To export a record type declaration from a module, replace the word declare with the word export:

  export record point
   field float x
   field float y

As with functions, you can export a record type pre-declaration:

  export record point elsewhere

As with functions, you must also export the record type declaration. The export status of the declaration and the pre-declaration must match.

Note that exporting a record type declaration makes it possible to declare variables of that type in the importing program or module, but it does not create or export a record of the declared type.

Exporting opaque record type declarations

You can also export an opaque record type declaration by following the word export with the word as opaque. If a record type declaration is exported as opaque, the importing program or module will not have access to the fields of record variables of that type. They will be able to create and manage variables of this type, but will not be able to access or manipulate their contents. Only code within your module will be able to address and access the fields of the records. This allows you to use a module to hide data and restrict operations on the data to thse supported by the exported functions.

  export opaque record point
   field float x
   field float y

Exporting the properties of modules imported by the current module

You can export some or all of the properties of a module that you have imported into your module. For instance, suppose that you created a module that exports a record type that uses floating point numbers for fields. The module will need to import the OMFLOAT library to allow fields of type float to be defined.

  module
  
  import "omfloat.xmd" unprefixed
  
  export as opaque record point
   field float x
   field float y

However, if a program that imports this module wants to access the fields of point records, it will also need access to the float data type and the operators that the OMFLOAT library provides for working with floating point numbers. Rather than requiring users of your module to import the OMFLOAT library separately, you can re-export its exported properties by including the word export before the prefix declaration:

  module shared as point.xmd
  
  import "omfloat.xmd" export unprefixed
  
  export record point
   field float x
   field float y

You can also re-export individual imported properties, or exclude them from being reexported by combining export with a use declaration.

Notice that if you re-exported the properties of a module that you had imported with a prefix, that prefix is also exported, so that if your module was defined as follows:

  module shared as point.xmd
  
  import "omfloat.xmd" export prefixed by fp.
  
  export record point
   field float x
   field float y

The importing program would have to use that prefix on the names of the float library properties, in addition to any prefix declared for your module:

  import "point.xmd" prefixed by pt.
  
  process
   local pt.point a
   local pt.fp.float x initial {23.7}
   local pt.fp.float y initial {54.9}
     
     set a:x to x
     set a:y to pt.fp.sqrt y