This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
object.t
339 lines (274 loc) · 6.4 KB
/
object.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#! perl
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$
=head1 Lua object
=head2 Synopsis
% perl t/object.t
=head2 Description
See "Programming in Lua", section 16 "Object-Oriented Programming".
=cut
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../../../lib", "$FindBin::Bin";
use Parrot::Test tests => 9;
use Test::More;
language_output_is( 'lua', <<'CODE', <<'OUT', 'object' );
Account = {balance = 0}
function Account.withdraw (self, v)
self.balance = self.balance - v
end
a1 = Account; Account = nil
a1.withdraw(a1, 100.00)
print(a1.balance)
a2 = {balance = 0, withdraw = a1.withdraw}
a2.withdraw(a2, 260.00)
print(a2.balance)
CODE
-100
-260
OUT
language_output_is( 'lua', <<'CODE', <<'OUT', 'object' );
Account = {balance = 0}
function Account:withdraw (v)
self.balance = self.balance - v
end
a = Account
a:withdraw(100.00)
print(a.balance)
CODE
-100
OUT
language_output_is( 'lua', <<'CODE', <<'OUT', 'object' );
Account = { balance = 0,
withdraw = function (self, v)
self.balance = self.balance -v
end
}
function Account:deposit (v)
self.balance = self.balance + v
end
Account.deposit(Account, 200.00)
print(Account.balance)
Account:withdraw(100.00)
print(Account.balance)
CODE
200
100
OUT
language_output_is( 'lua', <<'CODE', <<'OUT', 'classe' );
Account = {balance = 0}
function Account:new (o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Account:deposit (v)
self.balance = self.balance + v
end
function Account:withdraw (v)
self.balance = self.balance - v
end
a = Account:new{balance = 0}
a:deposit(100.00)
print(a.balance)
b = Account:new()
print(b.balance)
b:deposit(200.00)
print(b.balance)
CODE
100
0
200
OUT
language_output_is( 'lua', <<'CODE', <<'OUT', 'inheritance' );
Account = {balance = 0}
function Account:new (o)
-- print "Account:new"
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Account:deposit (v)
-- print "Account:deposit"
self.balance = self.balance + v
end
function Account:withdraw (v)
-- print "Account:withdraw"
if v > self.balance then error"insuficient funds" end
self.balance = self.balance - v
end
a = Account:new()
print(a.balance)
-- r, msg = pcall(Account.withdraw, a, 100)
-- print(msg)
SpecialAccount = Account:new()
function SpecialAccount:withdraw (v)
-- print "SpecialAccount:withdraw"
if self.balance - v <= -self:getLimit() then
error"insuficient funds"
end
self.balance = self.balance - v
end
function SpecialAccount:getLimit ()
-- print "SpecialAccount:getLimit"
return self.limit or 0
end
s = SpecialAccount:new{limit=1000.00}
s:deposit(100.00)
print(s.balance)
s:withdraw(200.00)
print(s.balance)
CODE
0
100
-100
OUT
language_output_is( 'lua', <<'CODE', <<'OUT', 'multiple inheritance' );
-- look up for 'k' in list of tables 'plist'
local function search (k, plist)
for i=1, #plist do
local v = plist[i][k] -- try 'i'-th superclass
if v then return v end
end
end
function createClass (...)
local c = {} -- new class
local arg = {...}
-- class will search for each method in the list of its
-- parents ('arg' is the list of parents)
setmetatable(c, {__index = function (t, k)
return search(k, arg)
end})
-- prepare 'c' to be the metatable of its instance
c.__index = c
-- define a new constructor for this new class
function c:new (o)
o = o or {}
setmetatable(o, c)
return o
end
-- return new class
return c
end
Account = {balance = 0}
function Account:deposit (v)
self.balance = self.balance + v
end
function Account:withdraw (v)
self.balance = self.balance - v
end
Named = {}
function Named:getname ()
return self.name
end
function Named:setname (n)
self.name = n
end
NamedAccount = createClass(Account, Named)
account = NamedAccount:new{name = "Paul"}
print(account:getname())
account:deposit(100.00)
print(account.balance)
CODE
Paul
100
OUT
language_output_is( 'lua', <<'CODE', <<'OUT', 'multiple inheritance (patched)' );
-- look up for 'k' in list of tables 'plist'
local function search (k, plist)
for i=1, #plist do
local v = plist[i][k] -- try 'i'-th superclass
if v then return v end
end
end
function createClass (...)
local c = {} -- new class
local arg = {...}
-- class will search for each method in the list of its
-- parents ('arg' is the list of parents)
setmetatable(c, {__index = function (t, k)
-- return search(k, arg)
return (search(k, arg))
end})
-- prepare 'c' to be the metatable of its instance
c.__index = c
-- define a new constructor for this new class
function c:new (o)
o = o or {}
setmetatable(o, c)
return o
end
-- return new class
return c
end
Account = {balance = 0}
function Account:deposit (v)
self.balance = self.balance + v
end
function Account:withdraw (v)
self.balance = self.balance - v
end
Named = {}
function Named:getname ()
return self.name
end
function Named:setname (n)
self.name = n
end
NamedAccount = createClass(Account, Named)
account = NamedAccount:new{name = "Paul"}
print(account:getname())
account:deposit(100.00)
print(account.balance)
CODE
Paul
100
OUT
language_output_is( 'lua', <<'CODE', <<'OUT', 'privacy' );
function newAccount (initialBalance)
local self = {balance = initialBalance}
local withdraw = function (v)
self.balance = self.balance - v
end
local deposit = function (v)
self.balance = self.balance + v
end
local getBalance = function () return self.balance end
return {
withdraw = withdraw,
deposit = deposit,
getBalance = getBalance
}
end
acc1 = newAccount(100.00)
acc1.withdraw(40.00)
print(acc1.getBalance())
CODE
60
OUT
language_output_is( 'lua', <<'CODE', <<'OUT', 'single-method approach' );
function newObject (value)
return function (action, v)
if action == "get" then return value
elseif action == "set" then value = v
else error("invalid action")
end
end
end
d = newObject(0)
print(d("get"))
d("set", 10)
print(d("get"))
CODE
0
10
OUT
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: