[NAME]
ALL.dao.data.invar

[TITLE]
Invariables

[DESCRIPTION]

In every places where var can be used, invar can be used as well, to declare local, glob
al or member invariables. 
     
   1  invar invariable = "global invariable"
   2  for(var i = 1 : 5 ) {
   3      invar invariable = 123
   4      if( i > 3 ) {
   5          invar invariable = "local invariable"
   6          io.writeln( invariable )  # Output: local invariable
   7      }
   8      io.writeln( invariable )  # Output: 123
   9  }
  10  io.writeln( invariable )  # Output: global invariable
     


Very much like constants, invariables cannot be modified once initialized. But unlike con
stants which must be initialized with constant expressions, invariables can be initialize
d with ordinary variables or expressions. Though the values of invariables cannot be modi
fied through the invariables themselves, they can still be modified through the original 
variables. 
     
   1  var   varlist = { 123 }
   2  invar invlist = varlist
   3  
   4  varlist.append( 456 )  # OK! Now, invlist = { 123, 456 }
   5  invlist.append( 456 )  # Error!
     


Additionally invar can be used in parameter list to declare invariable parameters. 
     
   1  routine Rout( invar abc = 123 )
   2  {
   3      abc += 1  # Error!
   4  }
     


Though invariables cannot be modified, they do can be reinitialized at their declaration 
sites. For example, 
     
   1  for(var i = 1 : 3 ){
   2      invar index = i  # OK!
   3      index += i       # Error!
   4  }
     


But for class instance invariables, they can be initialized anywhere inside the class con
structors. 
     
   1  class Klass
   2  {
   3      invar mapping = {=>}
   4  
   5      routine Klass( name: string, value: int ) {
   6          mapping = { name => value }  # OK!
   7      }
   8      routine Method() {
   9          mapping = { "abc" => 123 }  # Error!
  10          mapping[ "abc" ] = 123      # Error!
  11      }
  12  }
     

invar can also be used to declare class methods that do not modify their instance variabl
es. 
     
   1  class Klass
   2  {
   3      var value = 123
   4  
   5      routine Modify() {
   6          value += 456
   7      }
   8      invar routine Method() {
   9          Modify()      # Error!
  10          value += 456  # Error!
  11          return value + 456
  12      }
  13  }