@@ -127,11 +127,10 @@ these functions. In this section we learn what these functions parameters are.
127127 - ` t1 ` is the left operand of the plus operator
128128 - ` t2 ` is the right operand of the plus operator
129129
130- Here the new and slightly extended code using the new information. If you execute it and follow the printed output then
131- you should be able to understand what is what. Some more info on the parameters. The names of the parameters can be
130+ Let's extended code using the new information. If you execute it and follow the printed output along with the code, then
131+ you should be able to understand what is what. Some more info on the parameters - the names of the parameters can be
132132chosen freely. Also for the ` __call ` event you can replace the ` ... ` with named parameters.
133133
134-
135134 T = { }
136135 print("I'm table", T)
137136
@@ -165,7 +164,6 @@ chosen freely. Also for the `__call` event you can replace the `...` with named
165164 -- trigger the __add event
166165 local Sum = T + T
167166
168-
169167If you execute this it prints for example ...
170168
171169 I'm table table: 0x7ffd72d03f50
@@ -175,4 +173,71 @@ If you execute this it prints for example ...
175173 you are using the + operator on the tables 'table: 0x7ffd72d03f50' and 'table: 0x7ffd72d03f50'
176174
177175
178- The code isn't still doing much, but this will change in the next section.
176+ ## Implementing the behavior
177+
178+ You may have noticed that besides printing text on screen nothing is happening. The values didn't get created, nothing
179+ was assigned, etc. Sure that's simply because we only called ` print() ` in our functions, but we are responsible for
180+ implementing the desired behavior. The behavior is really up to you. If you only want to print e.g. an error message
181+ saying * 'this key does not exist'* for the ` __index ` event, then that's what you can do. Lua will not tell you how to
182+ use these events.
183+
184+ But let's assume we want to implement the things we would expect from the lines that trigger the events.
185+
186+ T =
187+ {
188+ Value = 55
189+ }
190+
191+ M =
192+ {
193+ __index = function(t,k)
194+ -- return some default value
195+ return 123
196+ end,
197+
198+ __newindex = function(t,k,v)
199+ -- here we need to use the rawset() function, because if we would
200+ -- write t[k]=v then we would trigger again __newindex, which would execute this function again and so on.
201+ -- rawset() does the same but without triggering __newindex, means it prevents such recursive calls.
202+ rawset(t,k,v)
203+ end,
204+
205+ __call = function(t,addme)
206+ -- as an example let's just add the parameter to T.Value, but you can code what you want
207+ T.Value = T.Value + addme
208+ end,
209+
210+ __add = function(t1,t2)
211+ -- I don't think this needs much explanation
212+ return t1.Value + t2.Value
213+ end,
214+ }
215+
216+ setmetatable(T, M)
217+
218+ -- trigger the __index event
219+ local Foo = T.foo
220+ print(Foo)
221+
222+ -- trigger the __newindex event
223+ T.Foo = "foo"
224+ print(T.Foo)
225+
226+ -- trigger the __call event
227+ T(12)
228+ print(T.Value)
229+
230+ -- trigger the __add event
231+ local Sum = T + T
232+ print(Sum)
233+
234+ and this prints ...
235+
236+ 123
237+ foo
238+ 67
239+ 134
240+
241+ The take away of this chapter is simply that we have to take care of the things we expect from these functions, and that
242+ for ` __newindex ` we have to use ` rawset() ` if we want to set the value in the table, otherwise we get recursive calls.
243+
0 commit comments