get_instance(+term, ?instance)

This predicate can be used to retrieve the instance that unifies with a meta goal. A meta goal is the value of a variable that is proven. The subject of a match can be an instance and if this is so the second argument is unified with that same instance.

When an instance of a class is created its default name is the name of the class, so if two instances of the class stack/2 are created two instance clauses will be added to the current module with the name 'stack' and two attributes. If the goal 'stack(_,_).' is proven then two solutions are found but it is not possible to send a message to the instance of each solution because the goal is not an instance. With get_instance/2 it is possible to get access to each instance of the class stack/2 that matched with the goal 'stack(_,_)'.

see also: add_callback/3 add_callback/4 callbacks class_name/2 clear_var/1 del_obj/1 instance/1 new_obj/2 new_obja/2  ispresent_callback/4 object-oriented prolog remove_callback/4 <-/2 ::<-/1 ::<-/2 this/1

Example
X=stack(I, O),
X,  %Metacall
get_instance(X, In),
In<-init. %Send message
X is unified with a filter to find instances of the class stack/2, then the goal is proven and a match is found, the clause instance that matched will be assigned to the variable 'In'

 

Exceptions
the first parameter is an empty variable an instantiation_error exception is thrown
the last parameter is not a variable with a value a type_error(variable, Arg) exception is thrown
the first parameter is a list an type_error(evaluable, Arg) exception is thrown
the last parameter is a list a type_error(instance, Arg) exception is thrown

 

 

info@trinc-prolog.com