Make .__index be able to yield

Is there any haxy way to make .__index yield from the side that is hosting the metatable?
like:


-- .__index yield code
local t = {}
do
	local mt = {}
	mt.__index = function(t,k)
	    wait(3)
	    return true
	end
	setmetatable(t,mt)
end

-- indexing (SHOULD NOT BE CHANGED)
print(t.a)

I don’t think it’s possible at all.

If you intend to use __index as a way to have properties, then it shouldn’t yield anyway. I expect indexing properties to be instantanous. Have it return a function that you can call instead.

4 Likes

Use the __call metamethod instead.

local t = {}
do
	local mt = {}
	mt.__call = function(t,k)
	    wait(3)
	    return true
	end
	setmetatable(t,mt)
end

print(t("a"))
4 Likes

Someone else proof check this for me since I’m not home, but I remember the handling of __index in the Lua source depends on its own “sub stack” when the method is called, which would get wayyyyy too messy to fix if you attempted to yield and throw it into some sort of thread scheduler.

Actually, my signal objects wrap methods that shouldn’t yield in an __index call when testing, so I’d argue it’s useful in some cases.

1 Like

You should like share how you do it :stuck_out_tongue_winking_eye:

It makes no sense for code to yield there. In the case of needing to yield through an iteration of indices, you would just create a special iterator/enumerator thing.

Typically things should be designed to index as quickly as possible, so why do you need to yield?

(Sorry if that came off kinda mean – I’m multitasking watching my roommate play the new monster hunter lol)

2 Likes

I don’t have a specific use case anymore, so now I’m just wondering out of curiousity

1 Like

It might be possible to seize up the client while it waits for something using a while loop, but Lua doesn’t use real threads so probably not.