Page 1 of 1
MiddleClass Equivalence
Posted: Sat Oct 30, 2010 6:31 am
by allumbra
Hello,
I recently discovered your MiddleClass lib and am wondering how you would implement object equivalence.
e.g.
local a = AnObject:new()
local b = a
if b==a then
print("objects are the same!")
end
Re: MiddleClass Equivalence
Posted: Sat Oct 30, 2010 6:57 am
by ninwa
Since you cannot overload operators in Lua (I think?) you would need to implement a :compare member which parsed each tables structure and compared each individual element.
Re: MiddleClass Equivalence
Posted: Sat Oct 30, 2010 8:48 am
by bartbes
You can overload operators, in this case you want to overload the __eq metamethod. (Problem is, if you don't know what it means, you'll need some reading up to do it.)
Re: MiddleClass Equivalence
Posted: Sat Oct 30, 2010 8:53 am
by Robin
ninwa wrote:Since you cannot overload operators in Lua (I think?) you would need to implement a :compare member which parsed each tables structure and compared each individual element.
ORLY? You might want to take another look at
chapter 13 of Programming in Lua, the revered PiL.
As for the OP, I'm not so sure. But I think you can use something like this
Code: Select all
--in a function, of course
for k, v in pairs(table1) do
if type(table2[k]) ~= type(v) then
return false
elseif type(v) == 'table' then
if not --[[thisFunction]](v, table2[k]) then
return false
end
else
if not v == table2[k] then
return false
end
end
end
return true
This fails on recursive data structures, btw.
Re: MiddleClass Equivalence
Posted: Sat Oct 30, 2010 10:43 am
by kikito
Hi there!
When did you download MiddleClass?
I noticed that problem with "pointer comparison" 10 days ago and made a small ammendment. The most recent version will run this code just fine.
Code: Select all
local a = AnObject:new()
local b = a
if b==a then
print("objects are the same!")
end
You still can override the behaviour of the __eq operator by defining the __eq method in your class.
Code: Select all
function AnObject:initialize(name)
super.initialize(self)
self.name = name
end
-- redefine __eq
function AnObject:__eq(other)
return self.name == other.name
end
-- create two different objects with the same name
local a = AnObject:new('peter')
local b = AnObject:new('peter')
if a == b
print("the objects are equal")
end
In any case, I don't recommend this. Pointer comparison is too useful; I recommend defining a :equals method instead (a:equals(b)) and leaving __eq alone.
Re: MiddleClass Equivalence
Posted: Sat Oct 30, 2010 12:03 pm
by Robin
... except that in Lua you don't have pointers, so it's more like reference equality than pointer equality.
Re: MiddleClass Equivalence
Posted: Sat Oct 30, 2010 12:39 pm
by kikito
Indeed. I meant "reference comparison" not "pointer comparison". Wrote in a rush.
Re: MiddleClass Equivalence
Posted: Thu Nov 04, 2010 2:26 am
by allumbra
Hey - that worked great. Apparently my lib was not up-to-date. Thanks for the prompt response and for the nice class lib
Sorry for the late reply - apparently I failed to submit my message previously
Re: MiddleClass Equivalence
Posted: Thu Nov 04, 2010 11:13 am
by ninwa
I've been taken to school, thank you for the correction guys.
Re: MiddleClass Equivalence
Posted: Fri Nov 05, 2010 8:17 pm
by Mud
kikito wrote:Indeed. I meant "reference comparison" not "pointer comparison". Wrote in a rush.
Well, the former is implemented in terms of the latter, so you weren't technical
wrong.