MadDogDean
MadDogDean

Reputation: 63

Call Lua function by reference

OK, I searched for this, but to no avail. It's kind of a vague thought/idea so... here goes.

Is it possible, in Lua, to call a function (with declared values) and then call it again by reference (without having to pass the values again)?

What I am trying to do is create a larger generic function (that is used repeatedly) that has the passed values declared when the generic function is called.

If the generic function fails, I want it to recycle and try to call itself with the same used values (without having to pass them to the function again.)

If I didn't lose anyone, hopefully there are a few thoughts & ideas out there. Tim

Upvotes: 2

Views: 1287

Answers (1)

Mud
Mud

Reputation: 28991

If you want arguments passed to a function, you need to pass them, every time.

You could bind some arguments to a function via a closure:

function bind(f, ...)
    local args = {...}
    return function()
        return f(unpack(args))
    end
end

foo = bind(print, "This", "is", "a", "test")
foo() --> This    is      a       test
foo() --> This    is      a       test

Would be a lot easier to answer your question if you posted some example code.

Upvotes: 4

Related Questions