lua-users home
lua-l archive

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


Sam Roberts wrote:
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 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.

Since there is only one metatable for strings, and a lot of Lua code
probably relies on string comparison being byte-for-byte (actually,
it's just a pointer comparison, so it's really fast), it would
almost certainly be a bad idea to change the equality comparison
function for all strings. In any event, Lua doesn't let you do that.

There's nothing stopping you from defining a caseless comparison
function as a string method:

> function string:alike(other) return self:lower() == other:lower() end
> s = "WomBat"
= s:alike"wombat"
true

That would be a bit faster written in C, of course.