Skip to content

Global and Local Names

A dfn does not declare its local names. Every name assigned within a dfn is automatically local to that dfn, so assigning to a name inside a dfn never changes a global of the same name.

Example

      x←99
      {x←1 ⋄ x} 0      ⍝ the inner x is local
1
      x                ⍝ the global x is unchanged
99

A name that is referenced but not assigned within the dfn is global: it is found in the enclosing environment. Because dfns nest lexically, a dfn defined inside another dfn can see the names local to the enclosing dfn – this is known as lexical name scope.

There is no header in which to localise names, and none is needed: automatic localisation takes the place of the explicit locals list of a traditional function. The system function ⎕SHADOW skips dfns when it searches the stack for a tradfn in which to localise a name.

Assigning to a Name in the Enclosing Environment

Assignment always creates a local name. A dfn can call a name that exist outside itself in one of two ways – by using modified assignment or by qualifying the name with a namespace path.

Modified assignment updates an existing name in place instead of creating a new one. With right () as the modifying function, the value is replaced outright. For example:

      x←99
      x
99
      {x⊢←1 ⋄ x} 0        ⍝ the global x is updated
1
      x
1

The name must already have a value; modified assignment to an undefined name signals a VALUE ERROR. Note also that a named function cannot be used as the modifying function in a dfn without ∘⊢; see Restrictions.

Qualifying the name with a namespace path is the more general mechanism, because it also creates a name that does not yet exist. ⎕THIS names the dfn's own home namespace. For example:

      {⎕THIS.y←1 ⋄ 0} 0   ⍝ y need not exist beforehand
0
      y
1

Either way, where the name is local to an enclosing dfn, it is that local that is updated, following Lexical Name Scope. Namespaces and Localisation describes how a qualified name is resolved.