lua-users home
lua-l archive

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


Hi,

I'd like to change the behaviour of __eq when applied to strings.  I
suspect I'll need to somehow change the underlying string type?

My attempt at this was along the lines of:

Lua 5.2.2  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> m={}
> function m:__eq(rhs)
>> print("in _eq")
>> return true
>> end
> t = {}
> setmetatable(t, m)
> if t == "string" then
>> print("true")
>> else
>> print("false")
>> end
false
> q = {}
> setmetatable(q,m)
> if t == q then
>> print("true")
>> else
>> print("false")
>> end
in _eq
true
>

while the problem could be with my code, I suspect it is related to this:

2.4 – Metatables and Metamethods
[...] "eq": the == (equal) operation. Behavior similar to the "add"
operation, except that Lua will try a metamethod only when the values
being compared are either both tables or both full userdata and they
are not primitively equal. The result of the call is always converted
to a boolean.

Anyway, does anyone have a pointer or example showing how to do
override __eq (and string operations more generally).

--[[

The objective here is to fully integrate my objects into Lua.  That
is, expressions such as:
  o = "string"
  i = i+1
  if i == 1 or o == "string" then
all appear to behave as expected yet, underneath, they are using
meta-methods such as __setindex, __add, and __eq to perform
operations.

]]

Andrew