    [NAME]
    ALL.misc.comparison.overloading

    [TITLE]
    Operator Overloading (Python)

    [DESCRIPTION]

    This help entry will demonstrate a few examples to compare Dao and Python in operator
    overloading for field accessing, subindexing and binary operator etc.

     0.1   Field Getter and Setter

       1  # Python
       2  class Something:
       3      def __init__(self):
       4          self.thing = {}
       5
       6      def __getattr__(self, name):
       7          if name == "something":
       8              return self.thing
       9          return None
      10
      11      def __setattr__(self, name, value):
      12          if name == "something":
      13              self.thing = value;

       1  # Dao
       2  class Something
       3  {
       4      var thing = {}
       5
       6      routine Something() {  # Not really needed;
       7          thing = {}
       8      }
       9      operator .something() {
      10          return thing
      11      }
      12      operator .something=( value ){
      13          thing = value;
      14      }
      15      # Or alternatively:
      16      operator .( name : string ) {
      17          if( name == "something" ) return thing
      18          return none;
      19      }
      20      operator .=( name : string, value ){
      21          if( name == "something" ) thing = value;
      22      }
      23  }

     0.2   Item Getter and Setter

       1  # Python
       2  class UserDict:
       3      def __init__(self):
       4          self.data = {}
       5
       6      def __getitem__(self, key):
       7          return self.data[key]
       8
       9      def __setitem__(self, key, item):
      10          self.data[key] = item

       1  # Dao
       2  class UserDict
       3  {
       4      var data = {=>}
       5
       6      routine UserDict() { # Not really needed;
       7          data = {=>}
       8      }
       9      operator []( key ){
      10          return data[key]
      11      }
      12      operator []=( item, key ){
      13          data[key] = item
      14      }
      15  }

    Please note that in operator[]=, item is the first parameter. It is arranged in this way
    to naturally support multiple keys/indices. For example, for a multidimensional array,
    one may define,

       1  class UserDict
       2  {
       3      var data = {=>}
       4
       5      operator []( index1, index2 ){
       6          return data[ index1, index2 ]
       7      }
       8      operator []=( item, index1, index2 ){
       9          data[ index1, index2 ] = item
      10      }
      11  }

     0.3   Arithmetic Operators

       1  # Python
       2  class MyNumber:
       3
       4      def __init__(self, value):
       5          self.value = value
       6
       7      def __add__(self, other):
       8          return MyNumber( self.value + other.value )

       1  # Dao
       2  class MyNumber
       3  {
       4      var value = 0
       5
       6      routine MyNumber( value = 0 ) {
       7          self.value = value
       8      }
       9
      10      operator +( other : MyNumber ) {
      11          return MyNumber( value + other.value )
      12      }
      13  }