MiddleClass Equivalence

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
allumbra
Prole
Posts: 2
Joined: Fri Oct 29, 2010 5:39 am

MiddleClass Equivalence

Post 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
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: MiddleClass Equivalence

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: MiddleClass Equivalence

Post 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.)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: MiddleClass Equivalence

Post 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.
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: MiddleClass Equivalence

Post 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.
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: MiddleClass Equivalence

Post by Robin »

... 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: MiddleClass Equivalence

Post by kikito »

Indeed. I meant "reference comparison" not "pointer comparison". Wrote in a rush.
When I write def I mean function.
allumbra
Prole
Posts: 2
Joined: Fri Oct 29, 2010 5:39 am

Re: MiddleClass Equivalence

Post 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
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: MiddleClass Equivalence

Post by ninwa »

I've been taken to school, thank you for the correction guys. :)
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: MiddleClass Equivalence

Post 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. :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot], tpimh and 0 guests