2005/5/10

     
 

CfgScript Variable Declaration Statements

artefaktur

| Expression statement | block {} Statement | Branch Statements | Loop Statements | Type Declaration | Variable Declaration | Synchronize Statements | With Statement | Using Statement | Type Alias |

Type declarations introduces new Variable in a Script


See also:  Types.

Content of this chapter:

   VarDecl
     Class Name Resolution
       Search in TypeAlias
       Search in Usings
       Search in Namespace
     Scoped Variables
     Typed Variable Declarations
     Untyped Variable Declarations
   Global variables




 VarDecl

VarDecl
: [  TypeName ] VarName [ '='  Expression ] ';'
;

A component type name identifies a enumeration or class name with optional namespace/package name.

 Class Name Resolution

The class name resolution works in following steps:

 Search in TypeAlias

CfgScript first looks in the TypeAlias tables if there is any alias for given TypeName.
If an alias was found the type name will replaced with the original type name.

See also:  Type Alias.

 Search in Usings

Next step is to search in the declared using in the scope.

See also:  Using Statement.

 Search in Namespace

If the type was not found in the further steps the type are searched in the list of loaded classes.

If no namespace name is given it search through all namespaces to find the given class.

If the same namespace exists in different namespaces the first found class will be choosen (no warning, no error!).

If it the class cannot be found it uses the ClassLoader to try to load the class.

For more information about ClassLoader please refer to  Library.


 Scoped Variables

The scope of variables are limited by their block:


int i = 42; // is global scope
{ // Block, introduce new scope
  
  int i = 1; // overwrite variable for this scope
  int j = 2; // j is only valid in this scope
  i == 1;
  j == 2;
} // leaving scope 
i == 42; // i is now global scope
j == 2; // invalid because j is not longer valid

 Typed Variable Declarations

Typed variables has a defined type

int i; // i is an int
acdk.lang.StringBuffer sb; // sb is StringBuffer
i = 3; // OK
i = 4.3; // OK, convert automatically
i = "A Text"; // throws ClassCastException
sb = new acdk.lang.StringBuffer(""); // OK
sb = new Integer(2); // throws ClassCastException


 Untyped Variable Declarations

In the default mode ad hoc variables usage, without a defined type of the variable is also possible:

i = 2; // i is a any-type (DmiObject) containing a byte
i = "Text"; // now i contains a string
Any j = 2; // Explicit Any type

See also:  PragmaStatement.
See also:  Any Type.

 Global variables

The Script Environment contains following global variables.

  • interpreter: instance of current  acdk::cfgscript::Script.
  • false: Boolean(false)
  • true: Boolean(true)
  • Nil, nil, null: Nil object
  • out: acdk.lang.System.out
  • err: acdk.lang.System.err
  • File_separator: acdk.io.File.separator
  • File_pathSeparator: acdk.io.File.pathSeparator