From my knowledge,
obj:method(...)
is syntax sugar (a nicer way to write something else) for this code:
obj.method(obj, ...)
So then how do function calls such as math.random(min, max)
not fail even if math
isn't passed as first argument?
And when calling random
as a method it fails with the error:
wrong number of arguments
If someone can explain to me how it works it would be appreciated.
So then how do function calls such as math.random(min, max) not fail even if math isn't passed as first argument?
Because when you call the method, its passing self
as the first argument to the random
function in the math
class. So you're essentially trying to call math.random(math, min)
when you do math:random(min, max)
local a = {} function a.b(x, y) print(x, y) end a.b(1, 2) a:b(1, 2)
This will output
1 2
table: 0x199f490 1