Contents | Index | < Browse | Browse >
2.2 Variables

  Variables are "containers" that hold data (like numbers or strings) that can
  be addressed by the variables name. To specify the type of the data,
  a variable is usually "declared" before it is used. During the lifetime of the
  variable, it cannot change its type.
    
  The most formal way to declare a variable is explicitly by using the "DEFTYPE"
  compiler directive and always use the type extension when the variable
  is used.

  +-----------------------------------------------------------------------------
  | 
  | DEFTYPE.t name, ...       ; declare variable name as type .t
  | name.t = expression       ; assign the result of the expression to name
  | 
  +-----------------------------------------------------------------------------
  Note: See section 2.5 Primitive Types and 2.6 Newtypes for more information
        and possible types.
  Note: See section 2.8 Constants how to write constant expressions such as
        numbers or strings.

  Using the same variable name with different type extentions will lead to a
  type-missmatch compiler error.
  Variable names are case sensitive and can contain the character a-z, A-Z,
  0-9 and "_" (under score), but are not allowed to start with a number.
  Variables are always initialized to zero or empty string, depending on the
  type. 
  There are three strictness settings that influence the declaration of
  variables. The compiler directive is called "Syntax n", were "n" can be
  0 (default), 1 or 2.
    
2.2.1 Syntax 0 (loose, default)

  Variables don't have to be explicitly declared. Their type can be specified
  during their first usage by mentioning the type extension. If they are used
  the first time without a type extension, they get the default type.
  The default type can be changed by using the compiler directive "DEFTYPE.t" 
  without declaring a variable. Per default, the default type is Quick (.q).

  Example:
  +-----------------------------------------------------------------------------
  |
  | a = 5.5             ; a is now type Quick (.q) and has the value 5.5
  | DEFTYPE.l           ; set the default type to Long (.l)
  | b = 6               ; b is now of type Long and has the value 6 
  | c.q = 1             ; c is now of type Quick and has the value 1
  | 
  +-----------------------------------------------------------------------------
  Note: This syntax mode is traditionally used in BASIC dialects. However,
        this is very unsafe if you plan a medium or large project. You can
        easily introduce errors by typos that occure during runtime, and not
        already at compile time.
        
2.2.2 Syntax 1 (strict)

  All variables have to be declared and the type extension has always to be
  mentioned.
  Note: This syntax mode is similar to Pascal. It is very safe against
        typos, but creates significant overhead and can be anoying.

2.2.3 Syntax 2 (medium, recommended)

  Variables don't have to be explicitly declared, but their type has be specified
  at least by mentioning their type extension on first usage.
  On later usage, the extension is optional.
  
  Example:
  +-----------------------------------------------------------------------------
  | 
  | Syntax 2
  | DEFTYPE.l a, b, c   ; declare a, b and c as type Long (and set to 0)
  | d.l = 1             ; declare d as type Long   and set it to 1
  | e.f = 1.0           ; declare e as type Float  and set it to 1
  | f.s = "test"        ; declare f as type String and set it to "test"
  | g   = 5             ; this will throw a compiler error, because g is not
  |                       explicitly declared and no type is mentioned.
  | a.f = 5             ; this will throw a compiler error, because a is 
  |                       already declared as type Long (.l)
  +-----------------------------------------------------------------------------
  Note: Don't mix the "Syntax" directive up with the so called
  "new syntax mode", which is enabled by the optimize 4 directive.

2.2.4 Assignment Shortcut

  As a shortcut, the assign operator "=" can be ommited if the first operand is
  also the variable to assign the result to. 

  Example:
  +-----------------------------------------------------------------------------
  |
  | a = a+1   ; normal writing
  | a + 1     ; this does the same, it's a short cut
  |
  | a = a/2+5 ; normal writing
  | a / 2+5   ; short cut
  |
  +-----------------------------------------------------------------------------
  Note: Unlike "a /= 2+5" in C/C++, "a / 2+5" would first devide though 2 and
        then add 5 (respecting common mathematical precedence).

2.2.5 Global vs. Local Variables
     
  Variables that are declared in the global context (outside of a
  Function/Statement) are global and have a lifetime that equals to the runtime
  of the program.
  Variables that are declared in a local context (inside a Function/Statement)
  are local and have a lifetime that starts and ends with the local context.

  By default, global variables are not reachable within local contexts. To
  "import" a global variable into a local context, the "SHARED" directive is
  used. If this is done inside a local context, the import is valid only for
  this specific context. If SHARED is used in global context, the variable is
  imported implicitly to every local context.

  Example:
  +-----------------------------------------------------------------------------
  |
  | DEFTYPE.l a  ; declare a global variable
  | SHARED b.l   ; declare a global variable that is reachable in all functions
  | 
  | Function.l test{}
  | SHARED a     ; a needs to be "SHARED"
  | c.l = a + b
  | Function Return c
  | End Function
  |
  +-----------------------------------------------------------------------------
  Note: "a" and "b" are both global variables. "a" needs to be explicitly
        imported, while "b" is imported automatically. "c" is a local variable
        that is not available in global context and its lifetime ends when the
        function exits.
  Note: The global usage of SHARED needs new syntax mode (optimize 4) to
        be switched on.
    

Contents | Index | < Browse | Browse >