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
MiddleClass Equivalence
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: MiddleClass Equivalence
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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: MiddleClass Equivalence
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.)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: MiddleClass Equivalence
ORLY? You might want to take another look at chapter 13 of Programming in Lua, the revered PiL.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.
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
Help us help you: attach a .love.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: MiddleClass Equivalence
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.
You still can override the behaviour of the __eq operator by defining the __eq method in your class.
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.
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
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
When I write def I mean function.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: MiddleClass Equivalence
... except that in Lua you don't have pointers, so it's more like reference equality than pointer equality.
Help us help you: attach a .love.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: MiddleClass Equivalence
Indeed. I meant "reference comparison" not "pointer comparison". Wrote in a rush.
When I write def I mean function.
Re: MiddleClass Equivalence
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
Sorry for the late reply - apparently I failed to submit my message previously
Re: MiddleClass Equivalence
I've been taken to school, thank you for the correction guys.
Re: MiddleClass Equivalence
Well, the former is implemented in terms of the latter, so you weren't technical wrong.kikito wrote:Indeed. I meant "reference comparison" not "pointer comparison". Wrote in a rush.
Who is online
Users browsing this forum: Google [Bot], tpimh and 2 guests