OSDN Git Service

【更新内容】
[ring-lang-081/ring.git] / docs / en / target / declarative.txt
1 .. index:: 
2         single: Declarative Programming; Introduction
3
4 ===============================================
5 Declarative Programming using Nested Structures
6 ===============================================
7
8 In this chapter we are going to learn how to build declarative programming world using nested structures on the top
9 of object oriented.
10
11 We will learn about
12
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
17
18 .. index:: 
19         pair: Declarative Programming; Objects inside lists
20
21 Creating Objects inside Lists
22 =============================
23
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.
26
27 Example:
28
29 .. code-block:: ring
30
31         alist = [new point, new point, new point]       # create list contains three objects 
32
33         alist + [1,2,3]                                 # add another item to the list
34
35         see "Item 4 is a list contains 3 items" + nl
36         see alist[4] 
37
38         add(alist , new point)
39         alist + new point
40
41         alist[5] { x = 100 y = 200 z = 300 }
42         alist[6] { x = 50 y = 150 z = 250 }
43
44         see "Object inside item 5" + nl
45         see alist[5]
46         see "Object inside item 6" + nl
47         see alist[6]
48
49         class point x y z
50
51 Output:
52
53 .. code-block:: ring
54
55         Item 4 is a list contains 3 items
56         1
57         2
58         3
59         Object inside item 5
60         x: 100.000000
61         y: 200.000000
62         z: 300.000000
63         Object inside item 6
64         x: 50.000000
65         y: 150.000000
66         z: 250.000000
67
68 .. index:: 
69         pair: Declarative Programming; Return object by reference
70
71 Composition and Returning Objects and Lists by Reference
72 ========================================================
73
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.
75
76 if the caller used the assignment operator, another copy of the object will be created.
77
78 The caller can avoid using the assignment operator and use the returned reference directly to access the object.
79
80 The same is done also if the attribute is a list (not object).
81
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 
84         will be done.
85
86 Example:
87
88 .. code-block:: ring
89
90         o1 = new Container
91         myobj = o1.addobj()     # the assignment will create another copy
92         myobj.x = 100
93         myobj.y = 200
94         myobj.z = 300
95         see o1.aobjs[1]         # print the object inside the container 
96         see myobj               # print the copy
97
98         Class Container
99                 aObjs = []
100                 func addobj
101                         aobjs + new point
102                         return aobjs[len(aobjs)]        # return object by reference
103
104         Class point 
105                 x  = 10
106                 y  = 20
107                 z  = 30
108
109 Output:
110
111 .. code-block:: ring
112
113         x: 10.000000
114         y: 20.000000
115         z: 30.000000
116         x: 100.000000
117         y: 200.000000
118         z: 300.000000
119
120 Example(2):
121
122 .. code-block:: ring
123
124         func main
125                 o1 = new screen  {
126                         content[point()] { 
127                                 x = 100 
128                                 y = 200
129                                 z = 300         
130                         }
131                         content[point()] { 
132                                 x = 50 
133                                 y = 150
134                                 z = 250         
135                         }
136                 }
137                 see o1.content[1]
138                 see o1.content[2]
139
140         Class Screen
141                 content = []
142                 func point
143                         content + new point
144                         return len(content)
145
146         Class point 
147                 x  = 10
148                 y  = 20
149                 z  = 30
150
151 Output:
152
153 .. code-block:: ring
154
155         x: 100.000000
156         y: 200.000000
157         z: 300.000000
158         x: 50.000000
159         y: 150.000000
160         z: 250.000000
161
162 Example(3):
163
164 .. code-block:: ring
165
166         func main
167                 o1 = New Screen  {
168                         point() {               # access the object using reference 
169                                 x = 100 
170                                 y = 200
171                                 z = 300         
172                         }
173                         point() {               # access the object using reference 
174                                 x = 50 
175                                 y = 150
176                                 z = 250         
177                         }
178                 }
179                 see o1.content[1]               
180                 see o1.content[2]
181
182         Class Screen
183                 content = []
184                 func point
185                         content + new point
186                         return content[len(content)]    # return the object by reference
187
188         Class point x=10 y=20 z=30
189
190 Output:
191
192 .. code-block:: ring
193
194         x: 100.000000
195         y: 200.000000
196         z: 300.000000
197         x: 50.000000
198         y: 150.000000
199         z: 250.000000
200
201
202 .. index:: 
203         pair: Declarative Programming; executing code after the end of object access
204
205 Executing code after the end of object access 
206 =============================================
207
208 We can access an object using { } to use object attributes and methods.
209
210 if the object contains a method called BraceEnd(), it will be executed before the end of the object access.
211
212 Example: 
213
214 .. code-block:: ring
215
216         New Point { See "How are you?" + nl }
217
218         Class Point x y z
219                 func braceend
220                         see "I'm fine, Thank you!" + nl
221
222 Output:
223
224 .. code-block:: ring
225
226         How are you?
227         I'm fine, Thank you!
228
229
230 .. index:: 
231         pair: Declarative Programming; Declarative programming on the top of Object-Oriented
232
233 Declarative Programming on the top of Object-Oriented
234 =====================================================
235
236 The next features enable us to build and use declartive programming environment using nested structures on the top of object oriented 
237
238 * using {} to access the object attributes and methods
239 * BraceEnd() Method
240 * returning objects by reference
241 * Setter/Getter Methods (optional)
242
243 Example: 
244
245 .. code-block:: ring
246
247         # Declartive Programming (Nested Structures)
248
249         Screen() 
250         {
251
252                 point() 
253                 {                       
254                         x = 100 
255                         y = 200
256                         z = 300         
257                 }
258
259                 point() 
260                 {                        
261                         x = 50 
262                         y = 150
263                         z = 250         
264                 }
265         }
266
267         # Functions and Classes
268
269         Func screen return new screen
270
271         Class Screen
272
273                 content = []
274
275                 func point
276                         content + new point
277                         return content[len(content)]    
278
279                 func braceend
280                         see "I have " + len(content) + " points!"
281
282         Class point 
283
284                 x=10 y=20 z=30
285
286                 func braceend           
287                         see self        
288
289 Output:
290
291 .. code-block:: ring
292
293         x: 100.000000
294         y: 200.000000
295         z: 300.000000
296         x: 50.000000
297         y: 150.000000
298         z: 250.000000
299         I have 2 points!
300
301 .. index:: 
302         pair: Declarative Programming; More Beatiful Code
303
304 More beautiful Code
305 ===================
306
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.
315
316 Example:
317
318 .. code-block:: ring
319
320         new Container 
321         {
322                 Point 
323                 { 
324                         x=10 
325                         y=20 
326                         z=30 
327                 }
328         }
329
330         Class Container
331                 aObjs = []
332                 point
333                 func getpoint
334                         aObjs + new Point
335                         return aObjs[len(aObjs)]
336
337         Class Point x y z
338                 func braceend
339                         see "3D Point" + nl + x + nl + y + nl + z + nl
340
341 Output
342
343 .. code-block:: ring
344
345         3D Point
346         10
347         20
348         30
349