swirl
Guide to OmniMark 8   OmniMark home
docs home 
IndexConceptsTasksSyntaxLibrariesLegacy LibrariesErrors
 
  Related Syntax   Related Concepts  
declaration/definition  

global, local, constant, field

 
 

Syntax

(global | local | field) type name
  (variable (to numeric-expression)? (initial-size numeric-expression)?
   | size numeric-expression)?
  (initial {expression (with key string-expression)?)+))?

constant type name (size numeric-expression)?
   (initial {expression (with key string-expression)?)+))

or

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

constant type name (size numeric-expression)?
  elsewhere


Argument definitions

type
An OmniMark variable type, one of stream, integer, switch, or a specific type of OMX component such as BCD or float
name
The name of the variable being declared.


Purpose

You can use the keywords global and local to declare variables of any supported data type. A variable 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 variable.

Three classes of data types are available in OmniMark

Global variables and constants 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 supplied to those modules.

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

Local variables are available in the local 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 variables of different types:

  global stream my-text
  global integer my-number
  global switch my-flag
  global tcp.connection connection ;declare as an OMX variable of type tcp.connection
  global point co-ordinates ;assumes that record type point is declared

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

  find letter+
     local integer my-number

Initializing variables

You can declare initial values for your variables:

  global stream my-text initial {"Mary had a little lamb."}
  global integer my-number initial {6}
  global switch my-flag initial {true}

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

  constant float pi initial {3.141592}

You can initialize variables and constants using dynamic expressions:

  global bcd foo initial {6.5}
  global bcd bar initial {foo + 9.3}
  global integer port-number initial {5300 + #args[3]}
  global tcp.service service initial {tcp.create-service on port-number}
  constant stream run-start initial {date "=h:=m:=s"}
  
  process
    local tcp.connection connection initial {tcp.accept-connection from service}

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

Shelves

You can declare fixed-size or variable-size shelves:

  global stream my-text size 3
  global integer variable

You can also declare the initial size of a variable-size shelf:

  local switch my-flags variable initial-size 0

Note that the initial size of a shelf declared variable is 0 in OmniMark 5 and all subsequent versions. It was 1 in previous versions of the language. If you have a program written for a previous version that assumes, rather than declaring, that the initial size of a variable sized shelf is 1, you should make this explicit by adding initial-size 1 to the declaration. The code will then work identically on all versions.

You can declare initial values 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.

  local stream animals size 4 initial {"cow", "dog", "pig", "hen"}
  local switch my-flags variable initial-size 3 initial {true, false, true}

You can also declare the initial key values for the values you declare:

  local switch my-flags variable initial-size 3
     initial {true with key "mary", false with key "lamb", true with key "school"}

You can omit the initial size of a variable shelf when declaring initial values. The initial size of the shelf will be set to the number of initial values specified:

  local switch my-flags variable
     initial {true with key "mary", false with key "lamb", true with key "school"}

Similarly, you are not required to specify size for a multi-item fixed-sized global, constant or local shelf. So the following is allowed:

  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, then you have a fixed sized shelf whose size is the number of items in the initial part.

You can specify an initial value that is empty, thus creating a shelf with zero items. This is useful if you want to create a constant shelf with no items:

  constant 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

You can initialize global variables from the command line using the -os, -define, -activate, -deactivate, or -integer 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 variables and constants

Global variables and constants can be pre-defined by using the keyword elsewhere instead of an initializer. (initial-size also cannot be specified in a pre-definition.) Local variables and fields cannot be pre-defined.

Default values

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

Variable declarations in modules

A global or constant declaration occurring in a module may be preceded by the export keyword. This makes that global variable or constant available to the importing module or program.

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

    Related Syntax
   elsewhere
   record, declare record
   export, export as opaque
 
Related Concepts
   Counter data type
   Integer data type
   Module parameterization
   Modules, defining
   Records
   Records, pre-defining
   Shelves
   Shelves: pre-defining globals and constants
   Switch data type
   Variables and Constants
 
 

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

OmniMark 8.2.0 Documentation Generated: March 13, 2008 at 3:33:48 pm
If you have any comments about this section of the documentation, please use this form.

Copyright © Stilo International plc, 1988-2008.