+class_instance <- +body_term
+callable_term <- +body_term

This operator can be used for sending a message to a class instance or to a set of instances that match with a goal. The right operand must always be the name of a method of a class. The left operand must be a variable that refers to a class instance or a goal that is used for finding matching class instances.

The effects of searching for a instance

If the left operand of <-/2 is not an instance then after a successfull match with a clause it is possible that empty variables of the instance are connected to values or variables of the goal. This can cause unexpected effects when a method called for the same instance assigns values to the variables of the instance, it is possible that the variables used for finding a match are changed or used for the unification. The following example demonstrates this:

Suppose : the subject of the match is an instance of the class stack/2 and the goal not.

Goal of match:  stack( List, Contains ).
Subject of match:  stack( __1, __2 ).

After a successfull match the variable __1 will be connected to List and __2 will be connected to Contains. This means that any value assigned to __1 is actually assigned to List and the same is true for __2 and Contains.

If then the method init/0 is called that unifies the instance variables__1 with 0 and __2 with an empty list then in fact the variables to which __1 and __2 are connected will get values and not the instance variables. This is an unexpected effect when working with instances.

To prevent this effect the <-/2 operator clears all values assigned to empty instance variables because of the match with an instance and all connections to non-instance variables.

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

Example
X new_obj stack, X<-init. send message 'init' to instance referred to by X
stack(2, _)<-push(a). send the 'push' message to all stack instances that match
with the goal 'stack(2, _)'
X=stack(_, _),
X,
get_instance(X, Y),
Y<-isEmpty(true).
first the '=' operator is used to fill the variable X with the goal 'stack(_, _)', then the meta goal X is executed, if a match is found and the subject of the match is a class instance the variable Y will refer to that instance, the message 'isEmpty' is then send to it

 

Exceptions  
left operand is an empty variable an instantiation_error exception is thrown
left operand is not an atom nor a variable a type_error(callable, Arg) exception is thrown, Arg is replaced by the value of the incorrect operand
operator is not used to call a method of an instance an existence_error(class, Arg) exception is thrown, Arg is replaced by the left operand of the <- operator

 

 

info@trinc-prolog.com