Utility Attributes

Three common member attributes join, validate, and withdraw are special utility functions of the class, and should be defined according to the following specifications.

Besides the utility attributes, a conditional class may have a number of common or auto member attributes. These are the member services provided by the class. Note that the first argument of any of the attribute function is always the class itself, so that there is never a need to resolve name conflict between different conditional classes. When an object tries to use any of the member services, validate will be called. Only upon success of validate, the service can be provided.

The following code defines an ellipse, a regular class and circle, a conditional class.

The validate function of circle tests if a value x is an ellipse and if x.a is equal to x.b.

The circle class has a member attribute function perimeter. To call it, we need to specify the conditional class, e.g., x.perimeter(circle).

The validate function is automatically called when the systems tries to evaluate x in circle or x.perimeter(circle).

global.ellipse = class
    public a = 4 in _R;
    public b = 3 in _R;
    auto  area = () -> pi * parent.a * parent.b;
    new = (a,b)->();
  end


global.circle = conditional
    common validate = function sys -> r
       if parent in global.ellipse && parent.a == parent.b
          r = 1;
       else
          r = 0;
      end
    end
    common perimeter = sys -> 2*pi * parent.a;
end

c2 = ellipse.new(9,9);
c2.perimeter(circle)
c1 in circle

The following defines a conditional class called ratclub. In order to join the ratclub class, an object must belong to the the Person class. Upon joining the ratclub class, each new member is giving a ratid, and some information of the member is recorded on file. Each time a member service is request, the recorded information will be verified. If the verification fails, the request for service is denied.

global.Person = class
     public name = "John Smith";
     public gender = "M" in {"M", "F"};
     public phone = "911";
     public birthdate = "06/01/1985";
     public address = "123 Ashgrove cres., Stonycreek, ON";
     private id = 92052;
     public changeid = function newid -> ()
                          parent.id = newid;
               end
     common alterid = function newid -> ()
                     parent.id = newid;
                 end
     auto getid  = () -> parent.id;
end;


global.ratclub = conditional
   database = newpointer(~);
   common join = function sys -> r
            if parent in global.Person
                parent.ratid = # (sys.database)>> + 1;
                (sys.database) >> # (parent.ratid) = {name =
	   			 parent.name, phone = parent.phone, address =
	   			 parent.address, gender = parent.gender, birthdate =
	   			 parent.birthdate, points = 500};
                r = 1;
            else
                r = 0;
            end
	     end

   common validate = function sys -> r
             ratid = parent.ratid;
             record = (sys.database) >> # ratid;
                 if (record.name == parent.name && record.gender ==
	 				      parent.gender && record.birthdate == parent.birthdate
	 				      && record.address == parent.address &&
	 				      record.phone==parent.phone)
                 r = 1;
             else
                 r = 0;
             end
	      end

    common contribute = function (sys, x) -> r
                     ratid = parent.ratid;
                     ((sys.database) >> # ratid).points += x;

                     r = 1;
	              end

 
    common check_points = function sys -> r
                     ratid = parent.ratid;
                     dbp = sys.database;
                     db = dbp>>;
                     record = db # ratid;
                     r = record.points;
	              end

end

x = Person.new();
x.join(ratclub);
x.check_points(ratclub)

oz 2009-12-22