[top] [up] [next]

Creating new instances


Creating a new instance of a class is possible by using the new_obj/2 or the new_obja/2 operator. The left operand of the operator must be an empty variable and the right operand must be the name of a class or a variable containing the name of a class. If the arguments of the operator are valid a new instance is created and the left variable will contain a reference to the new instance. A new clause is asserted to the current module. Creating a new instance is like executing assertz, the new instance is also inserted into the current active module as the last clause. If an instance was created by the operator new_obja/2 it is inserted at as the first clause of the module.

The functor of the new clause will be the name of the class, this is the default behavior. Below there is an example of creating a class instance of the class stack.

  X new_obj stack. %Inserted at bottom
  X new_obja stack. %Inserted at top of module

All the normal prolog rules of behavior apply to class instances, instances can be treated as normal prolog clauses. It is possible to match prolog predicates with class instances and vice versa. The main difference when working with class instances is that the Trinc-Prolog development environment displays some extra information for each prolog clause that is a class instance. The name of the class to which the instance belongs is displayed in front of the name of the instance.

If the goal to prove is 'X new_obj stack.' then below the output of the Trinc-Prolog development environment is shown.

  X=stack.stack(__2, __3)
  %Instance of the class 'stack' and the name of the instance is 'stack'

 

arrow.gif (1632 bytes) If a module is closed that contains a class declaration then the interpreter checks, while closing the module, if the class has any instances that still exist, if this is the case then the module is not allowed to close, i.e. the class declaration is necessary to be able to work with instances of the class.

Giving instances a different name

Each class instance must have a name, the default name assigned to a new instance is the name of the class. If, for instance, ten instances of the class menu are created then there will be ten clauses in the module with the functor 'menu'.

It is possible to specify another name for a new instance when it is being created. The operator obj_name/2 must be used for this, below there is an example of creating a new instance of the class stack and changing it's name to 'mystack'.  

  X new_obj stack obj_name mystack. %New instance that is called 'mystack'

The result of executing this statement looks like:

  X=stack.mystack(__2, __3)
  %Instance of the class 'stack' and the name of the instance is 'mystack'

Assigning an unique name to an instance is useful for finding a class instance with the matcher. To find the instance created by the new_obj statement from the example above the goal must be 'mystack(Var1,Var2).'

Giving instances a unique name

It is possible with the operator unique_name/2 to have each instance get a unique name. The unique name is automatically generated by Trinc-Prolog and the type of the generated name is atom. The right operand of unique_name/2 must be an empty variable because the generated name is unified with that argument. For example:

  X new_obj stack unique_name Name.

A new instance will be generated, the values of the variables X and Name after the statement can look like:

  X = stack.o18708232
  Name = o18708232

[top] [up] [next]

 

info@trinc-prolog.com