@@ -107,3 +107,72 @@ Now we have learned the very basics of *metatables*, but this is the most import
107107concept. At the moment the functions don't do much except for the printing, and therefore we will learn in the next chapter
108108about the parameters of the * "event-action"* functions we just created.
109109
110+
111+ ## The Function Parameters
112+
113+ We were able to trigger the event functions, but what I did not tell you is that Lua also passes some parameters to
114+ these functions. In this section we learn what these functions parameters are.
115+
116+ - ` __index = function(t, k) print(t,k) end `
117+ - ` t ` is the table of which we tried to access the non-existing key
118+ - ` k ` is the non-existing key
119+ - ` __newindex = function(t, k, v) print(t,k,v) end `
120+ - ` t ` is the table for which we tried to add a new element
121+ - ` k ` is the key we tried to create
122+ - ` v ` is the value we wanted to assign
123+ - ` __call = function(t, ...) print(t,...) end `
124+ - ` t ` is the table we called like a function
125+ - ` ... ` are all parameters passed in parantheses
126+ - ` __add = function(t1,t2) print(t1,t2) end `
127+ - ` t1 ` is the left operand of the plus operator
128+ - ` t2 ` is the right operand of the plus operator
129+
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
132+ chosen freely. Also for the ` __call ` event you can replace the ` ... ` with named parameters.
133+
134+
135+ T = { }
136+ print("I'm table", T)
137+
138+ M =
139+ {
140+ __index = function(t,k)
141+ print(string.format("the key '%s' does not exist in table '%s'", k, t))
142+ end,
143+ __newindex = function(t,k,v)
144+ print(string.format("you want to create a new key '%s' in table '%s' and assign the value '%s'", k, t, v))
145+ end,
146+ __call = function(t,...)
147+ print(string.format("you are calling '%s' like a function with the parameters", t), ...)
148+ end,
149+ __add = function(t1,t2)
150+ print(string.format("you are using the + operator on the tables '%s' and '%s'", t1, t2))
151+ end,
152+ }
153+
154+ setmetatable(T, M)
155+
156+ -- trigger the __index event
157+ local Foo = T.foo
158+
159+ -- trigger the __newindex event
160+ T.foo = "bar"
161+
162+ -- trigger the __call event
163+ T(1,2,3)
164+
165+ -- trigger the __add event
166+ local Sum = T + T
167+
168+
169+ If you execute this it prints for example ...
170+
171+ I'm table table: 0x7ffd72d03f50
172+ the key 'foo' does not exist in table 'table: 0x7ffd72d03f50'
173+ you want to create a new key 'foo' in table 'table: 0x7ffd72d03f50' and assign the value 'bar'
174+ you are calling 'table: 0x7ffd72d03f50' like a function with the parameters 1 2 3
175+ you are using the + operator on the tables 'table: 0x7ffd72d03f50' and 'table: 0x7ffd72d03f50'
176+
177+
178+ The code isn't still doing much, but this will change in the next section.
0 commit comments