Builtin predicates - Classes and objects

Predicates in this group are used to create classes and objects, and to manipulate them after creation.

class, new, new_class/4, new_object/3


class, new_class/4 - declares a new class.

Usage
class Name { Members }
class Name : Parent { Members }
new_class(Name, Parent, Vars, Clauses)
Description
The Name must be an atom to identify the new class that is different than all previous atoms used as class names. If Parent is present, then it must be an atom that identifies an existing class, or [] for no parent.
Usually classes are created in the source file with the class keyword. The new_class/4 predicate can be used to create a class dynamically.
Vars must be a list of atoms for the member field names. These members will be added as unbound variable properties to new objects of the class Name. Duplicates in Vars will be ignored.
Clauses must be a list of clause(MemberName, Kind, Clause) terms:
  • MemberName is the name of the predicate member, without the class Name as a qualifier.
  • Kind is one of the atoms member, static, or constructor, indicating an ordinary member predicate, a static predicate, or an object constructor.
  • Clause is a (:-)/2 functor term for a single clause within the predicate being defined. The clause should have the predicate name Name::MemberName. If the Kind is member or constructor, then the first argument of Clause will be passed the Self object when the predicate is called.
The atoms className and prototype are reserved and cannot be used as member names for declared variable or predicate members.
Class Members may have one of the following forms, corresponding to member variables, regular member predicates, static predicates, and constructor predicates:
 var name1, ..., nameN
 name(Args) { ... }
 static name(Args) { ... }
 new(Args) { ... }
When an object is constructed with the new keyword, it is initially populated with name1, ..., nameN set to unbound variables, and prototype set to the class object. One of the constructor predicates is then called to initialize the object.
Regular member predicates and constructor predicates are passed the Self object as a hidden first argument. Static predicates do not have any hidden arguments.
Errors
Examples
 class vehicle
 {
     var owner, wheels
     transferOwnership(NewOwner)
     {
         Self.owner := NewOwner;
     }
 }

 class passenger_car : vehicle
 {
     var make, model
     new(Make, Model)
     {
         Self.wheels = 4;
         Self.make = Make;
         Self.model = Model;
     }
 }

 class company
 {
     var name
     var fleet
     new(Name)
     {
         Self.name = Name;
         Self.fleet = [];
     }
     add_vehicle(Vehicle)
     {
         Self.fleet := [Vehicle|Self.fleet];
     }
     remove_vehicle(Vehicle)
     {
         remove(Vehicle, Self.fleet, List);
         Self.fleet := List;
     }
 }

 class truck : vehicle
 {
     var company
     new(Company, Wheels)
     {
         Self.company = Company;
         Self.owner = Company.name;
         Self.wheels = Wheels;
         Company.add_vehicle(Self);
     }
     transferOwnership(Company)
     {
         Self.company.remove_vehicle(Self);
         Self.company := Company;
         vehicle::transferOwnership(Self, Company.name);
         Company.add_vehicle(Self);
     }
 }

 class semi_trailer : truck
 {
     new(Company)
     {
         truck::new(Self, Company, 16);
     }
 }

 new passenger_car(P, "MegaCarz", "FastKar 2000");
 P.owner = "Fred";
 P.transferOwnership("Mary");

 new company(C1, "Package Delivery, Inc.");
 new company(C2, "Deliver Packages, Inc.");
 new semi_trailer(S, C1);
 S.transferOwnership(C2);
See Also
class/1, new

new, new_object/3 - create a new object instance of a class.

Usage
new Name(Var, Args)
new_object(Name, Var, ArgList)
Description
The new keyword constructs a new instance of the class Name and unifies it with Var. The comma-separated list of Args is passed to the constructor for Name.
The new_object/3 predicate performs the same operation for constructing objects dynamically. The ArgList is a regular Plang list rather than a comma-separated list.
Errors
Examples
 class foo
 {
     new(X, Y) { ... }
 }
 class bar
 {
 }

 new foo(F, 1.5, f(X))
 new bar(B)
 new_object(foo, F, [1.5, f(X)])
 new_object(bar, B, [])
See Also
class, object/1

Generated on 26 May 2011 for plang by  doxygen 1.6.1