Shelves: pre-defining globals and constants

Global variables and constants may be pre-defined so that they may be referenced before their initializers are known. This provides the programmer with more options for organizing code. Pre-definitions must be used when declaring variables or constants in an interface module.

A shelf pre-definition is indicated by the keyword elsewhere:

  constant stream month-names size 12 elsewhere

When month-names is finally defined, it must have exactly 12 values:

  constant stream month-names size 12 elsewhere
  
  define function output-month-names as
    repeat over month-names
      output month-names || '%n'
    again
  
  ;...
  constant stream month-names size 12 initial
  {
    "January", "February", "March",
    "April", "May", "June",
    "July", "August", "September",
    "October", "November", "December"
  }
  
  process
    output "Months:%n"
    output-month-names

In this case, the function output-month-names must be defined some time after the pre-definition for month-names because it makes reference to it. It cannot be executed until month-names is defined, because that is what makes the values on the shelf available.

A shelf pre-definition must match the definition in every way, except that it cannot specify initial values, and, if the shelf is variable-sized, the pre-definition cannot specify an initial-size. One way to think about this, at least for global shelves, is that the pre-definition specifies those properties of the shelf that cannot be changed later.

Examples:

  constant month-names size 12 elsewhere; pre-definition
  global stream user-names variable elsewhere ; pre-definition
  global stream admin-users variable elsewhere ; pre-definition
  global stream months-with-r variable to 12 elsewhere; pre-definition
  
  constant stream month-names size 12 initial
  {
    "January", "February", "March",
    "April", "May", "June",
    "July", "August", "September",
    "October", "November", "December"
  }
  
  global stream user-names variable ; definition
  global stream admin-users variable initial {"harold", "maude"}
  global stream months-with-r initial-size 0 variable to 12
  process-start
    repeat over month-names
      set new months-with-r to month-names
        when month-names matches unanchored ul "r"
    again
  

Prerequisite Concepts