lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

In Lua, if you have an __index such as:

__index = function(t,k)
  return t[k]

It'll just call itself recursively. However, if Lua tagged tables with in_index and in_newindex, then you wouldn't be able to do something like this:

__index = function(t,k)
  if k == 0 then return 1 end
  return t[k-1]

So simply tagging it like that is not an option either.

Anyway, let's talk about the self-contained issue.

__gc should be self-contained. This is because __gc spews errors everywhere. For example, if you call debug.getinfo from __gc you get an assertion, debug hooks aren't called in __gc, etc. Shouldn't this property apply to other metamethods?

I guess with __index, you'd want to tag the table with both the __index function it's running and the key, and for __newindex, you'd want the __newindex function, the key, and the value. If they're all the same, chances are you'll get infinite recursion, in which case Lua should use raw operations.

This is an issue because you don't always want rawset or rawget to be available but you also don't want to cripple metatables too much.

Idk where I'm going with this. I guess my question is, why do metamethods have to rely on raw*?

Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.