Table.find and __eq metamethod

Does table.find() search for your value based off of the __eq metamethod, or does it just rawequals() the passed value with each value in the table and disregard anything you have in the __eq metamethod?

The table library (afaik) does not invoke metamethods unless you run some operator or unknown index during your code manually. It will run against your table as if it were vanilla otherwise. Unfortunately because table isn’t open-sourced we can’t read its implementation. That said, you can just test yourself whether or not table.find invokes __eq.

local Set = {}
Set.__index = Set
Set.__eq = function()
	error("Eq called.")

	setmetatable(t, Set)
	return t

local thisSet ={"Foo", "Bar"}
local thisSet2 ={"Baz"}

print(table.find(thisSet, "Foo"))
-- [1], did not invoke __eq.

print(table.find(thisSet, "Baz"))
-- nil, did not invoke __eq.

print(thisSet == thisSet2)
-- invokes __eq.

Does this answer your question or did I misunderstand? table.find has no reason to invoke __eq because you aren’t conditionally assessing the table to anything else. __eq will only invoke against two tables that hold the same metatable.

1 Like

You are way off. table.find returns the index of the first occurrence of the value (provided by the second argument) in the table (provided by the first argument).

How does this answer the post? The OP is not asking what table.find does.

For some reason I thought this post was asking about table.remove() and I typed table.find() anyway, just ignore my reply. I replied to the wrong thread.