Thursday, September 20, 2012

ELENA 2012: Interaction with Objects

As in the most of dynamic object-oriented languages the main way to interact with objects in ELENA is sending a message. Unlike others there are only limited set of possible message names (verbs, e.g. add, set, get, run, seek and so on). Though it is possible to provide the message namespace (subject). The message may contain a parameter (otherwise nil symbol is passed). When several parameters should be passed an argument list can be declared. If the object want to handle the message it has to contain the method with the same name. If no method mapping was found the flow is considered to be broken and the control goes to the next alternative flow or the program is stopped. Some generic messages (verbs without subjects) have alternative names (operators) which have a different parsing order.

The simple code to send a generic method looks like this:

'program'output write:"Hello World".

Note: "write" is a generic message, a literal constant is a parameter.

Several messages can be send in one statement, the parameter itself may be result of object interactions:

'program'output write "2 + 2 =" write:(2 add:2).

We could use operators to have the shorter code:

'program'output << "2+2=" << 2 + 2.

Note: "<<" is a synonym to "write" and "+" to "add".

Generic messages is used if we apply the operation to the whole object, if we need to work with a part of the object (roughly similar to a class property) we have to use qualified message.

'program'output << """Hello world"" contains " 
   << "Hello world" std'dictionary'length'get << " characters".

This code can be simplified. If the subject module is already defined at the source code beginning, its namespace may be skipped. Secondly in "get" messages (if the parameter is not provided) the verb part can be omitted:

#define std'dictionary'*.

[...]

'program'output << """Hello world"" contains " 
   << "Hello world" length << " characters".

In all examples above we use only a single parameter. But what to do if we need to pass several ones? The simple solution would be to create a special objects which will aggregate these parameters - argument list:

aDictionary append: { dictionary_key = "foo". content = "bar". }.

Note that the target object should interact with the parameter to get required data, hence the parameter becomes an active part of the interaction (this feature can be used in method multiple dispatching for example).

We could use a stack allocated argument list and simplify the code:

aDictionary append &dictionary_key:"foo" &content:"bar"

To be continued...

No comments:

Post a Comment