Anvil | Smiths Smiths | Register Register | Login Login |
Search:
Show links Show tools Show tree | Previous document Next document | njet.org > Anvil > Documentation > Language reference > Statements > Definition statements > Variables

Variables

Variables can be declared inside scripts (known as module variables) and classes (known as member variables).

Similar looking variable "pseudo declarations" can also occur inside functions, having slightly different meaning. Default behaviour inside functions is to refer to an already declared entity, and if none is found, new local variable is declared inside function. Sometimes this behaviour needs to be overridden: placing ``var´´ before entity reference will explitcly signal that this entity should be regarded as local variable, even if there would be entity with same name in some of the enclosing scopes. Modifier is typically used in assigments and ``foreach´´ variables slots, although they can be used in any identifier expression. Just note that initial value of variables is ``undefined´´.

If initializer expression evaluates to constant value at compile time it is called static initializer, otherwise it is called non-static initializer. Typically expression involving only operations between literals evaluates to constants.

All entities referred before initialization contains ``undefined´´. Different variable types are initialized as follows:

Module variables: Non-static initializer of module variables (and constants) are placed into hidden initialization function in order of declaration, depth first when descending into classes and interfaces. Therefore it is important to maintain the order of declarations such that referred entity is declared before referrer.

Member variable: Member variable are initialized in the beginning of constructor, before call to super class constructor.

Local variable: Local variables are initialized during the first assignment.

  variable      := "var" variable-decl ( "," variable-decl )* ";" 
  variable-decl := symbolname [ "=" exprinitializer ] 
See also  | Definition statements  | Classes  | Interfaces  | Modules  | <var>  | Grammar  | Namespaces
Contributes notes:
Add a note
What's new | Anvil