global, local, constant, field

declaration/definition

Syntax
domain-bound? global type name (variable (to numeric-expression)? (initial-size numeric-expression)?
                                | size numeric-expression)?
                               (initial shelf-literal | elsewhere)?


(local | field) type name (variable (to numeric-expression)? (initial-size numeric-expression)?
                           | size numeric-expression)?
                          (initial shelf-literal)?

constant type name (size numeric-expression)?
                   (initial shelf-literal | elsewhere)?

      

Argument definitions

type
An OmniMark data type.
name
The name of the shelf being declared.
shelf-literal
A shelf literal.


Purpose

You can use the keywords global and local to declare shelves of any supported data type. A shelf is a data container that can have its value changed at any time.

The keyword constant is used to declare constants of any supported data type. A constant is a data container that can have its value set once when it is created, and cannot be changed later.

The keyword field declares a field in a record. A record can contain one or more fields, and each field is a data container, like a shelf.

Three classes of data types are available in OmniMark:

global and constant shelves are available across your entire program. However, globals declared inside a module are not visible outside that module, unless exported, and globals declared in the main program are not visible inside the modules it imports, unless exposed to those modules using supply.

A global shelf declaration can be prefixed by keyword domain-bound. The actions save and save-clear treat such shelves differently from ordinary global variables, but otherwise they have the same properties.

local shelves are available in the scope in which they are created. All constants are global and must be declared in a global context. Local constants are not permitted.

The following code declares four global shelves of different types:

  global integer        i
  global string         s
  global switch         w
  global tcp.connection c 
  global point          x
          
The last two of these assume that the types tcp.connection and point have been declared.

The following code declares a local shelf in the scope of a find rule:

  find letter+
     local integer j
          

Initializing shelves

Initial values can be assigned to shelves when they are declared:

  global integer i initial { 6 }
  global string  s initial { "Mary had a little lamb." }
  global switch  w initial { true }
            

A constant must be initialized when it is declared because its value cannot be changed:

  constant float x initial { -1.0 }
            

Shelves and constants can be initialized using dynamic values:

  global   bcd         x           initial { 6.5 }
  global   bcd         y           initial { x + 9.3 }
  global   integer     port-number initial { 5300 + #args[3] }
  global   tcp.service service     initial { tcp.create-service on port-number }
  
  constant string      birth-time  initial { date "=h:=m:=s" }
  
  process
     local tcp.connection connection initial { tcp.accept-connection from service }
            

Any value used in a dynamic initializer must itself be initialized before it is used.

Shelves

Shelves can be declared to have a fixed-size or a variable-size shelves:

  global integer i variable
  global string  s size 3
            

A variable-size shelf can also be given an initial size:

  process
     local switch w variable initial-size 5
            

The default initial size of a variable-size shelf is 0.

Initial values can be declared for both fixed-size and variable-size shelves. For variable-size shelves, the number of initial values must match the number of items on the shelf. For fixed-size shelves, the number of initial values cannot exceed the size of the shelf.

  process
     local integer fibonacci variable initial-size 9 initial { 0, 1, 1, 2, 3, 5, 8, 13, 21 }
     local string  animals   size 4                  initial { "cow", "dog", "pig", "hen" }
            

Keys can also be assigned to the initial values that are declared:

  process
     local switch mammals variable initial-size 3 initial { true  with key "cow", 
                                                            false with key "hen", 
                                                            true  with key "dog" }
            

The initial size of a variable-size shelf can be omitted when initial values are specified. The initial size of the shelf will be set to the number of initial values specified:

  process
     local switch mammals variable initial { true  with key "cow", 
                                             false with key "hen", 
                                             true  with key "dog" }
            

Similarly, the size of a multi-item fixed-sized shelf does not need to be specified:

  constant integer weekday-number initial { 0 with key "monday",
                                            1 with key "tuesday",
                                            2 with key "wednesday",
                                            3 with key "thursday",
                                            4 with key "friday",
                                            5 with key "saturday",
                                            6 with key "sunday" }
            

If variable is not specified and no size is given, but an initial part is specified, the resulting shelf has a fixed-size equal to the number of items in the initial part.

The initializer can be empty, thus creating a shelf with zero items. This is useful for creating a constant shelf with no items:

  constant integer empty-integer-shelf initial { }
            

A constant cannot be variable in size. If a size is specified, it must be the number of items in the initial part.

Initializing variables from the command line

global shelves can be initialized from the command line using the -os, -define, -activate, -deactivate, or -counter command-line options. A shelf with an initial size other than 1 cannot be initialized from the command line. constants and globals decared in modules cannot be initialized from the command line.

Pre-defining shelves and constants

globals shelves and constant shelvess can be pre-defined by using the keyword elsewhere instead of an initializer. initial-size cannot be specified in a pre-definition. local shelves and fields of record types cannot be pre-defined.

Default values

The default values of OmniMark's built-in shelvf types are as follows:

  • the items of a shelf of type stream are initialized to unattached,
  • the items of a shelf of type string are initialized to the empty string,
  • the items of a shelf of type integer are initialized to 0, and
  • the items of a shelf of type switch are initialized to false.

Shelf declarations in modules

A global or constant declaration occurring in a module may be preceded by the export keyword. This makes that global shelf or constant shelf available to the importer of the module.

A global declaration in a module can be preceded by require to indicate that a global shelf of the same type must be provided to it using supply. The importer of the module must provide the required global using a supply clause on the import declaration. require declarations may not indicate any size or initial value information.