2 single: Declarative Programming; Introduction
4 ===============================================
5 Declarative Programming using Nested Structures
6 ===============================================
8 In this chapter we are going to learn how to build declarative programming world using nested structures on the top
13 * Creating Objects inside Lists
14 * Composition and Returning Objects and Lists by Reference
15 * Executing code after the end of object access
16 * Declarative Programming on the top of Object-Oriented
19 pair: Declarative Programming; Objects inside lists
21 Creating Objects inside Lists
22 =============================
24 We can create objects inside lists during list definition.
25 Also we can add objects to the list at any time using the Add() function or the + operator.
31 alist = [new point, new point, new point] # create list contains three objects
33 alist + [1,2,3] # add another item to the list
35 see "Item 4 is a list contains 3 items" + nl
38 add(alist , new point)
41 alist[5] { x = 100 y = 200 z = 300 }
42 alist[6] { x = 50 y = 150 z = 250 }
44 see "Object inside item 5" + nl
46 see "Object inside item 6" + nl
55 Item 4 is a list contains 3 items
69 pair: Declarative Programming; Return object by reference
71 Composition and Returning Objects and Lists by Reference
72 ========================================================
74 When we use composition and have object as one of the class attributes, when we return that object it will be returned by reference.
76 if the caller used the assignment operator, another copy of the object will be created.
78 The caller can avoid using the assignment operator and use the returned reference directly to access the object.
80 The same is done also if the attribute is a list (not object).
82 .. note:: Objects and Lists are treated using the same rules. When you pass them to function they are passed by reference,
83 when you return them from functions they are returned by value except if it's an object attribute where a return by reference
91 myobj = o1.addobj() # the assignment will create another copy
95 see o1.aobjs[1] # print the object inside the container
96 see myobj # print the copy
102 return aobjs[len(aobjs)] # return object by reference
168 point() { # access the object using reference
173 point() { # access the object using reference
186 return content[len(content)] # return the object by reference
188 Class point x=10 y=20 z=30
203 pair: Declarative Programming; executing code after the end of object access
205 Executing code after the end of object access
206 =============================================
208 We can access an object using { } to use object attributes and methods.
210 if the object contains a method called BraceEnd(), it will be executed before the end of the object access.
216 New Point { See "How are you?" + nl }
220 see "I'm fine, Thank you!" + nl
231 pair: Declarative Programming; Declarative programming on the top of Object-Oriented
233 Declarative Programming on the top of Object-Oriented
234 =====================================================
236 The next features enable us to build and use declartive programming environment using nested structures on the top of object oriented
238 * using {} to access the object attributes and methods
240 * returning objects by reference
241 * Setter/Getter Methods (optional)
247 # Declartive Programming (Nested Structures)
267 # Functions and Classes
269 Func screen return new screen
277 return content[len(content)]
280 see "I have " + len(content) + " points!"
302 pair: Declarative Programming; More Beatiful Code
307 We can get better results and a more beautiful code when we can avoid writing () after the method name
308 when the methods doesn't take parameters.
309 This feature is not provided directly by the Ring language because there is a difference between object methods
310 and object attributes. We can get a similar effect on the syntax of the code when we define a getter method for
311 the object attribute. For example instead of defining the point() method. we will define the point attribute then
312 the getpoint() method that will be executed once you try to get the value of the point attribute.
313 since we write the variable name direcly without () we can write point instead of point() and the method getpoint()
314 will create the object and return the object reference for us.
335 return aObjs[len(aObjs)]
339 see "3D Point" + nl + x + nl + y + nl + z + nl