This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple.lua
More file actions
168 lines (132 loc) · 2.98 KB
/
multiple.lua
File metadata and controls
168 lines (132 loc) · 2.98 KB
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
-- shared_meters = true
keep_ambiguous = true
require 'unclasslib'
function say(msg) print(msg and "> " .. msg or "") end
print [[
> meter = class()
> function meter:__init(value)
> print("meter init " .. (value or 'nil'))
> self.value = value or 0
> end
> function meter:set(value)
> self.value = value
> end
> function meter:read()
> print("meter read " .. self.value)
> return self.value
> end
> function meter:which()
> print("meter = " .. tostring(self) .. " { value = " .. self.value .. " }")
> end
> meter.value = -1
]]
meter = class()
function meter:__init(value)
print("meter init " .. (value or 'nil'))
self.value = value or 0
end
function meter:set(value)
self.value = value
end
function meter:read()
print("meter read " .. self.value)
return self.value
end
function meter:which()
print("meter = " .. tostring(self) .. " { value = " .. self.value .. " }")
end
meter.value = -1
m = shared_meters and shared(meter) or meter
mm = shared_meters and "shared(meter)" or "meter"
say("hygro = class(" .. mm .. ")")
hygro = class(m)
print [[
> function hygro:read()
> print("humidity " .. self[meter].value)
> end
]]
function hygro:read()
print("humidity " .. self[meter].value)
end
say("therm = class(" .. mm .. ")")
therm = class(m)
print [[
> function therm:read()
> print("temperature " .. self[meter].value)
> end
]]
function therm:read()
print("temperature " .. self[meter].value)
end
print [[
> hygrotherm = class(hygro, therm)
> function hygrotherm:read()
> self[hygro]:read()
> self[therm]:read()
> end
]]
hygrotherm = class(hygro, therm)
function hygrotherm:read()
self[hygro]:read()
self[therm]:read()
end
say('ht = hygrotherm(0)')
ht = hygrotherm(0)
say()
say("ht[therm]:set(11)")
ht[therm]:set(11)
say("ht[therm]:read()")
ht[therm]:read()
say()
say("ht[hygro]:set(22)")
ht[hygro]:set(22)
say("ht[hygro]:read()")
ht[hygro]:read()
say()
say("ht[therm]:read()")
ht[therm]:read()
say()
say("ht:read()")
ht:read()
say()
say("ht:which()")
status, msg = pcall(ht.which, ht)
if not status then print(msg) end
function show(t)
if not t or not t.__type then print("Sorry, cannot show " .. tostring(t)) return end
print('__type', t.__type)
for i, v in pairs(t) do
if type(i) ~= 'string' or i:sub(1, 2) ~= '__' then print(i, v) end
end
end
print()
print("Class attributes:")
print()
print('meter:') show(meter)
print()
print('therm:') show(therm)
print()
print('hygro:') show(hygro)
print()
print('hygrotherm:') show(hygrotherm)
print()
print("ht is a meter = ", ht:is_a(meter))
print("ht is a therm = ", ht:is_a(therm))
print("ht is a hygro = ", ht:is_a(hygro))
print("ht is a hygrotherm = ", ht:is_a(hygrotherm))
print()
print("Object attributes:")
print()
print("ht:") show(ht)
print()
print("ht[hygro]:") show(ht[hygro])
print()
print("ht[therm]:") show(ht[therm])
print()
print("ht[hygro][meter]:") show(ht[hygro][meter])
print()
print("ht[therm][meter]:") show(ht[therm][meter])
print()
print("ht[meter]:")
status, msg = pcall(show, ht[meter])
if not status then print(msg)end