Declaring __call metamethod with a promised, non-Anonymous function

As the topic states. I want to set the __call metamethod to a non-Anonymous function (is there a specific word for this? I have no idea!) AND I want that function to be yet to be defined in the code. So essentially I want to make the following to work.

local Data = {}
Data.__index = Data
Data.__call = Data.new

function Data.new()
    local d = setmetatable({},Data)
    return d
end

as opposed to using an anonymous function like this:

local Data = {}
Data.__index = Data
Data.__call = function()
    local d = setmetatable({},Data)
    return d
end

OR fully defining the function before hand like this:

local Data = {}
Data.__index = Data

function Data.new()
    local d = setmetatable({},Data)
    return d
end

Data.__call = Data.new

You can wrap the meta method in a new anonymous function so that Data.new is not evaluated until it’s called:

local Data = {}
Data.__index = Data
Data.__call = function(t, ...) Data.new(...) end

function Data.new()
    print("creating")
    local d = setmetatable({},Data)
    return d
end

local d = Data.new() --> creating
local d2 = d() --> creating

Personally I would just use your third option though, it seems cleaner.

2 Likes

This works pretty much perfectly for my cause thank you.
You’re not wrong, it’s definitely more elegant but I really like the “bookkeeping” aspect of keeping the declarations together near the top.
It just helps me keep things in line with my brain better I suppose.