lua-users home
lua-l archive

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


On Fri, Mar 16, 2007 at 08:37:13PM -0300, Ignacio Burgueño wrote:
> Is it possible to change strings __eq metamethod? I'd like to change it 
> so I can do caseless comparisons.
> I know that in order to do that, I need to do it from C,

I don't think you have to do it from C. If one of the value-types
doesn't have a metatable, you have to add it from C, but strings already
have a metatable.

If you follow though the pseudo-code in the docs for the eq metamethod
in http://www.lua.org/manual/5.1/manual.html#2.8, it seems like all the
conditions are met:

Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
> getmetatable("").__eq = function (...) print(...); return true; end
> l = "hi"
> r = "bye"
> = type(l)
string
> return  type(r)
string

-- The type is the same.

> = (l == r)
false

-- They are not primitive eq.

> mm1 = getmetatable(l).__eq
> mm2 = getmetatable(r).__eq
> = mm1
function: 0x807f088
> = mm2
function: 0x807f088

-- they have the same handler for eq

> = mm1(l,r)
hi      bye
true

-- and it is callable

> = l == r
false

-- But no, the metamethod isn't triggered by the operator.

I suspect the behaviour is as intended, and that this is a doc bug,
unfortunately, or else somewhere else in the refman there are a few
crucial words that would make it clear that this is not expected to
work.

Sam