Andrew Cagney
2015-06-16 15:42:51 UTC
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
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
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)
setmetatable(t, m)
if t == "string" then
falsefunction m:__eq(rhs)
print("in _eq")
return true
end
t = {}return true
end
setmetatable(t, m)
if t == "string" then
print("true")
else
print("false")
end
else
print("false")
end
q = {}
setmetatable(q,m)
if t == q then
in _eqsetmetatable(q,m)
if t == q then
print("true")
else
print("false")
end
else
print("false")
end
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