NeturalMath Language Definition and Syntax

Constraining Variables

Variables in NeturalMath may exist in private, public, or global scope (See Domains and Scope and Variables and Scope for more information). Users of NeturalMath may want to limit how variables in public or global scope are used by code outside of that domain. Alternatively, users may want to ensure that once a value has been set, it cannot be modified. NeturalMath provides mechanisms to ensure that developers can limit how a variable is used.

Constraining variables using constants

A developer may wish to assign a variable to a specific value, and never let it change once it has been set. The const keyword allows the developer to do this (See User Defined Constants for more information). The example below shows declaring a variable as a constant.
// simple constant
const x = 10

// constant using an expression
const y = x + sin(x)

// using a constant in an expression
z = y * x
print y

// the following lines create an error because the constant cannot be modified
x = 3
y = 12

Constraining variables using readonly

There may be times when it is necessary to modify a variable from within the same domain, but not allow code outside the domain to change it. The readonly keyword allows the developer to set this (See readonly for more information). The readonly key word effectively make the variable behave like a constant to all code outside of the domain, but behave like a local variable to code inside the domain. The following code example illustrates this concept.
demo = {
     public readonly myProperty = 10
     
     // it is ok to modify the property from the same domain
     myProperty = 5
}

// we can read the variable outside the domain
y = demo.myProperty + 12

// this line creates an error because it is not possible to modify the property
demo.myProperty = 2


See Also

User Defined Constants
const
readonly

Last edited Oct 18, 2010 at 1:30 PM by zanethorn, version 6

Comments

No comments yet.