This project is read-only.

NeturalMath Language Definition and Syntax

Variables and Scope

All variables in NeturalMath have a given scope. This scope may be public, private, or global (See Domains and Scope for more information). By default, all variables are declared in private scope. This means that the variable is only visible within the current domain. Unless the public or global keywords are provided, the variable may only be used by other statements within the current domain. The private keyword is redundant (all variables are automatically private unless stated otherwise), but is provided both out for completeness as well as to satisfy developers who want to use it to make their code more understandable. The scoping keywords may only be applied to explicit variables. Implicit variables must always be private in scope. All variables must be assigned a value before leaving the domain. If an implicit variable is not assigned a value before the domain is closed, the system will return an error.
// create a new domain to contain our variables
NewDomain = {  
     myLocal = 10           // private (by default) variable myLocal
     private myPrivate = 10 // variable explicitly defined as private
     public MyPublic = 10   // variable declared as public
     global MyGlobal = 10   // variable declared as global
     x = y + 10             // private (explicit) variable x and private (implicit) variable y
     y = 12                 // Assigning a value to y

Using Scoped Variables

The example above shows how to declare variables of differing scope. When using a variable, it is important to understand what scope the variable has. If a variable is private, the variable cannot be used outside of the domain in which it was declared. If a variable is public, it may be used outside of the domain, provided the domain reference is used to qualify it. If a variable is global, it may be used anywhere within the application that contains the domain.
The code below demonstrates how scoping works with variables using the domain created in the example above.
NewDomain = {
...see example above...

print myLocal             // creates an error (variable not found)
print newDomain.myLocal   // also creates an error (attempt to access private member)
print newDomain.myPrivate // same error as above line

print newDomain.MyPublic  // prints the value of myPublic (10)
newDomain.MyPublic = 12   // redefines myPublic as a value of 12
print newDomain.MyPublic  // prints a value of 12

print newDomain.MyGlobal  // prints the value of myGlobal (10)
print MyGlobal            // same result as the line above
MyGlobal = 12             // redefines myGlobal to a value of 12
print newDomain.MyGlobal  // prints 12

Parent vs. Child Scope

The example above shows code making use of calling variables declared in a child domain from the parent domain. Essentially what we have done is call into what is known as a child scope from a parent scope. This is performed by using the name of the declared domain. If we want to go in the opposite direction (calling a variable declared in the parent domain's scope from a child domain) we can optionally forgo the name of the parent domain. The example below illustrates this concept.
// declare some variables in the root domain

bob = 'bob'
mary = 'mary'
jane = 'jane'

// Declare a new domain
Managers= {
     dave = 'dave'
     sally = 'sally'
     tom = 'tom'

     // implicitly calling a variable from the root domain
     public TheBoss = mary

    Couples = {
          bobAndSally = bob + sally
          public DaveAndMary = dave + mary

          tomAndTheBoss = tom+ TheBoss
          bobAndTheBoss = bob + Managers.TheBoss

See Also

Domains and Scope

Last edited Oct 16, 2010 at 5:59 PM by zanethorn, version 4


No comments yet.