[top] [up] [next]

Tips for using instances


1. After an instance was created by new_obj/2 or new_obja/2 a variable contains a reference to it, it is possible to pass that variable as an argument to other clauses.

2. An instance can be assigned an unique name by the operator obj_name/2, see also creating new instances.

3. It is possible to create a goal and send a message to each instance that matches with the goal. For instance, the class stack has two variables, if you want to select all stack instances that are empty and push an element onto the stack you can use the following statement:  

    stack([], 0)<-push(a).

4. You can build more complex goals by using metavariables and the built-in predicate get_instance/2, for instance: suppose you want to select all stack instances that have not been initialized:

    X=stack(Li, Co),
    X,
    var(Li),
    get_instance(X, Instance),
    Instance<-init.

First the variable X is filled with the expression 'stack(Li, Co)', then a metagoal is executed to find all instances that match, with the built-in predicate var/1 the value of the variable is checked to see if it is empty. The predicate get_instance/2 is used to retrieve the subject of the match that matched with the metagoal, the subject is the instance that matched. After the instance was retrieved the 'init' message is send to it.

5. The instance for which a method of a class is being executed can be retrieved by the predicate this/1.

6. To determine which objects are still in memory you can use the save_module predicate (save_module/1, save_module/2, save_module/3). In the file that is created are all clauses including any instances crated by new_obj.

[top] [up] [next]

 

info@trinc-prolog.com