The Basic Types

    The basic types char, boolean, byte, integer, long, and real are also classes (although special) and therefore objects. Each of these classes defines a cast method for type  conversion and some other methods with information about the class itself such as the maximum value of an integer number. See the following example.

var i : integer;
var r : real = 5.0;
var j : byte;
  // no automatic conversion
i = integer.cast(r);
for j = byte.getMinValue() to byte.getMaxValue()/2 do
  Out.writeln( char.cast(j) );

    If integer.cast could not convert the value of r to integer at run time, an exception AssertionCastIntegerException would be thrown.

    The basic types (char, boolean, byte, integer, long, real, double) have value semantics. That means a variable of a basic type does not refer to an object as a variable whose type is a class. A variable
     var v : char;
really contains a character.

    Classes Char, Boolean, and the like are wrapper classes for the basic types. They inherit from AnyClass (and therefore from Any). Class Char is shown below

class Char
  public:
    proc init(ch : char)
      begin
      value = ch;
      end
    proc get() : char
      begin
      return value;
      end
  proc toString() : String
     begin
     return value.toString();
     end
  private:
    var value : char;
end

    The conversion from a value of a basic type to an object of  the corresponding  wrapper class is automatic (and vice-versa). See the code below.

    var I : Integer;
        i : integer;
begin
i = 1;
I = 1;
i = I;
I = i;
I = Integer.new(5);
i = Integer.new(5);
i = i + I;
I.set(3);
i = 6*I + 10 - I;
I = 3*i*I;
end
 

    If one wants to see everything as an object, she can replace the basic types by the wrapper classes. This would result in a very uniform (although inefficient) program, a kind of functionality only found in pure object-oriented languages as Smalltalk.

 

What is new ?

    The automatic conversion from an object of a wrapper class to a value of the corresponding basic type and vice-versa. This feature allows the program to consider everything an object. Then, if one wants she may program in Green as if it were a pure object-oriented language. The addition of methods like getMinInteger to class objects representing the basic types is also new although not very important.

Return