Message sending and diffusion

 

Message sending

J methods are executed when messages are sent to managed objects.  Messages are sent in the following way:

 

Object_name.method_Name();

 

 

We are going to send, for example, the "print" message to an instance of "Class".

 

Class c= ...

c.print();

 

 

Diffusion

Diffusion is a special case of message sending.  It has the following characteristics:

·         syntactic representation is ".<"

·         the diffusion of a message to an empty reference or to an empty set is equivalent to a null operation (noop)

·         the diffusion of a message to a set provokes the sending of a message to all its elements, instead of the destination set when a message is sent

·         an expression containing a diffusion has the empty value (convertible into all types) if the diffusion cannot be carried out

Diffusion and sending a message to a non empty reference on a simple object are equivalent.

 

This mechanism avoids using "for" loops and empty reference tests.

 

 

Example of diffusion

We are going to create a specific "print" method, on the Feature class and then diffuse it to the features (members) of a class in the "printFeature" method.  The "print" method uses a "format" method to format the name of the feature.

 

Note:      This example can be executed on the classes using the "printFeature" command.

 

 

Class:printFeature()

{

   PartFeature.<print();

}

 

Feature:print();

{

   // The current object is a Feature

   StdOut.write("Feature : ", format(), NL);

}

 

String Feature:format () return

{

   return Name;  // Very simple formatting...

}

 

Diffusion can also be used in simple expressions:

 

String n = c.<Name; // n is worth "" if c is an empty reference

 

 

General view of the "message concept"

A message can be generalized in anything that can provide a result:

·         getting an attribute value (for example, "Name")

·         getting associated elements (for example, "PartAttribute")

·         calling a J method

As a result, a J method call, or access to an attribute or to a related element, can be used uniformly in the same kind of expressions.