Does the following () search for __pairs metadata?

In general, the syntax is:

for k, v in pairs(t) do .... end 

equivalent to:

 for k, v in next, t do .... end 

But what if t has a __pairs __pairs ? Will the next() reference function be tested? If not, is it not better to always use pairs when iterating over tables and never call next() directly?

+8
lua metatable meta-method
source share
1 answer

No, next() does not check __pairs . The manual does not say.

It can be double verified from the corresponding source code, compare luaB_pairs and luaB_next .

There are times when you do not want to check the __pairs __pairs , so why do you always use pairs over next ?

+5
source share

All Articles