lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Thu, Feb 23, 2023 at 2:05 AM Spar <developspartv@gmail.com> wrote:
The same reason why we have : in the first place.
Taking self twice might be complicated if the variable not on stack.

Picture this:

a.b.c.d.foo(a.b.c.d, ...)
you have to get the value d twice, when colon operator copies the left value to the stack again

Lua is a small language with a lot of power, and the main reason for it is that it has not included the many 100s of proposals like this that have passed by in the last 20+ years.
In this case a little helper function can get rid of most of the inconvenience. If the overhead bugs you then introduce a local variable that you assign the value of "a.b.c.d" in your example.


local function dcall(ob, method, ...)
    return ob[method](ob, ...)
end

local tweedledee = {name = "tweedledee"}

function tweedledee:hello()
    print("hello", self.name)
end

local box = { thing = tweedledee }

local method = 'hello'

dcall(box.thing, method)