What are object types?

Description

Object types define dynamic collections of values, each of which either identifies an instance of an object described by the object type or is the special value nil, which is guaranteed not to identify any instance of an object. The values in object type collections are stored in variables, associated with the object type, called object references. These object references can be used to refer to or access the object instance identified by the value stored in the object reference.

Object instances have properties (which are similiar to the fields in records), and methods. Method are functions or procedures that define what the object can do and also what can be done to the object.

Irie Pascal does not currently fully support object types, in particular Irie Pascal does not allow you to define your own object types. You are restricted to using the built-in object types connection and recordset, or using the generic object type.

The built-in procedure new can be used to create an instance of one of the built-in object types, and store the value identifying the instance in an object reference. This also adds the identifying value to the collection of values defined by the built-in object type associated with the object reference. The built-in procedure new can not be used to create instances of generic object type, since generic object type do not describe what an object instance "looks" like (new wouldn't know how much memory to allocate).

The built-in function createobject can be used to create an instance of a generic object type, and returns the value identifying the instance. The value returned by createobject is usually stored in a generic object reference. NOTE: The built-in function createobject can not currently be used to create an instance of any of the built-in object types.

The built-in procedure dispose should be used to destroy instances of the built-in object types created with new, when you have finished using the instances. This also removes the value identifying the destroyed instance from the collection of values defined by the built-in object type.

The built-in procedure dispose should also be used to destroy instances of generic objects created with createobject. This also removes the value identifying the destroyed instance from the collection of values defined by the generic object type.

Example

For example the simple program below illustrates how to use the built-in procedures new and dispose with object types.

program Data(input, output);
var
   conn : connection;

   procedure DisplayError(msg : string);
   begin
      writeln('ERROR:', msg);
      halt
   end;

   function GetConnectionString : string;
   var
      s, sDSN, sUSER, sPassword : string;

      function GetStringValue(sPrompt : string) : string;
      var
         sValue : string;
      begin
         write(sPrompt);
         readln(sValue);
         GetStringValue := sValue
      end;

   begin
      if supported(feature_odbc) then
         begin
            sDSN := GetStringValue('Enter Data Source Name (DSN):');
            sUser := GetStringValue('Enter user id:');
            sPassword := GetStringValue('Enter password:');
            s := 'ODBC;DSN='+sDSN+';user='+sUser+';password='+sPassword;
         end
      else if supported(feature_mysql) then
         begin
            sUser := GetStringValue('Enter user id:');
            sPassword := GetStringValue('Enter password:');
            s := 'MYSQL;user="'+sUser+'";password="'+sPassword+'";socket="/tmp/mysql.sock"';
         end
      else
         DisplayError('No database support detected');
      GetConnectionString := s;
   end;

begin
   new(conn);
   conn.open(GetConnectionString);
   //
   //Add code here to process database
   //
   conn.close;
   dispose(conn);
end.

Syntax

The syntax for accessing an object property is give below

(NOTE: for clarity some parts of the syntax are omitted, see Irie Pascal Grammar for the full syntax):

   property-designator = object-variable '.' property-specifier

   object-variable = variable-access

   property-identifier = identifier

   property-specifier = property-identifier | '(' property-string ')'

   property-string = string-expression

where string-expression is an expression of type string which evaluates to the name of the property. The preferred form of a property reference is just to use the property name, the second form where a string expression is used is provided for the cases where the property name is a keyword. You can see this in the cdonts example below in which the property named to could not be specified as

   objMail.to

since to is a keyword, instead this property is specified using the second form like so

   objMail.('to')

The syntax for calling object method functions and procedures is given below:

   function-method-designator = object-variable '.' function-method-identifier [ actual-parameter-list ]

   procedure-method-statement = procedure-method-specifier [ actual-parameter-list ]

   actual-parameter = expression | variable-access |
       procedure-identifier | function-identifier

   actual-parameter-list = '(' actual-parameter { ',' actual-parameter } ')'

   function-method-identifier = identifier

   object-variable = variable-access

   procedure-method-identifier = identifier

   procedure-method-specifier = object-variable '.' procedure-method-identifier