Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do calls such as math.random(min, max) work without passing itself as first argument?

Asked by 5 years ago

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.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Question

So then how do function calls such as math.random(min, max) not fail even if math isn't passed as first argument?

Whats happening.

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)

Example

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

Ad

Answer this question