lua-users home
lua-l archive

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




On Thursday, June 12, 2014, Rena <hyperhacker@gmail.com> wrote:
On Wed, Jun 11, 2014 at 7:43 PM, Kalafut, Bennett <bennett.kalafut@thermofisher.com> wrote:

>    Inspection of the lua.org ltablib.c confirms that this isn't just a problem with the embedded Lua we're using: the official code uses lua_rawgeti for array access and doesn't fall back on the metamethod at all. 

One thing that's often bugged me in Lua is the imbalance between table access methods:

if I want to look up a string, I can use lua_getfield() as a shortcut, instead of lua_pushstring() and lua_gettable().
If I want to look up an integer, I can use lua_rawgeti().
but, one of these invokes metamethods, and one doesn't. Why is that? I keep missing a "lua_geti()" that retrieves an integer and does invoke metamethods. (and then there ought to be a lua_rawgetfield() for consistency.)

--
Sent from my Game Boy.

The manual says it is (paraphrasing) "because metamethods are rarely used on numeric indexes." I wonder if this belief is still held or if this is a hold over to an era before __len, __ipairs and __pairs...

I agree. It seems small, but I just had this discussion today. Because the two are imbalanced, our main C++ guy won't use getfield. He got burned by the difference with rawgeti and decided to just use the two-step process, *because it is consistent*.  And now all of his code is that way and it is worse than if the set/getfield didn't exist at all. He still needs to be aware of it in other code. 

Of course my_luaL_set/getindex is easy to write. But if you're the kind of person that is put off by small differences in what you think should be complimentary functions, you're also likely to be conservative with macros, patching and extending. 

-Andrew