Ring/Lessons/Declarative Programming using Nested Structures

Declarative Programming using Nested Structures
In this chapter we are going to learn how to build declarative programming world using nested structures on the top of object oriented.

We will learn about


 * Creating Objects inside Lists
 * Composition and Returning Objects and Lists by Reference
 * Executing code after the end of object access
 * Declarative Programming on the top of Object-Oriented

Creating Objects inside Lists
We can create objects inside lists during list definition. Also we can add objects to the list at any time using the Add function or the + operator.

Example:

Output:

Composition and Returning Objects and Lists by Reference
When we use composition and have object as one of the class attributes, when we return that object it will be returned by reference.

if the called used the assignment operator, another copy of the object will be created.

The caller can avoid using the assignment operator and use the returned reference directly to access the object.

The same is done also if the attribute is a list (not object).

.. note:: Objects and Lists are treated using the same rules. When you pass them to function they are passed by reference, when you return them from functions they are returned by value except if it's an object attribute where a return by reference will be done.

Example:

Output:

Example(2):

Output:

Example(3):

Output:

Executing code after the end of object access
We can access an object using { } to use object attributes and methods.

if the object contains a method called BraceEnd, it will be executed before the end of the object access.

Example:

Output:

Declarative Programming on the top of Object-Oriented
The next features enable us to build and use declarative programming environment using nested structures on the top of object oriented


 * using {} to access the object attributes and methods
 * BraceEnd Method
 * returning objects by reference
 * Setter/Getter Methods (optional)

Example:

Output:

More beautiful Code
We can get better results and a more beautiful code when we can avoid writing after the method name when the methods doesn't take parameters. This feature is not provided directly by the Ring language because there is a difference between object methods and object attributes. We can get a similar effect on the syntax of the code when we define a getter method for the object attribute. For example instead of defining the point method. we will define the point attribute then the getpoint method that will be executed once you try to get the value of the point attribute. since we write the variable name direcly without we can write point instead of point and the method getpoint will create the object and return the object reference for us.

Example:

Output