Symmetry of Box's equals method

30 views
Skip to first unread message

Jan Ouwens

unread,
Mar 1, 2012, 4:07:27 PM3/1/12
to Lift
Hi all,

I was playing around with Lift a bit (I'm using it in a new project)
when I found something unexpected in the net.liftweb.common.Box class;
namely that its equals method is not symmetric. See this gist for a
test that shows the problem: https://gist.github.com/1953157

If foo and boxedFoo are supposed to be equal, then I would expect the
second and third test to both pass; if they are not, then I would
expect both tests to fail. Either way, I expect the first test to
pass.

The equals method can be made symmetric pretty easily by removing the
second case in the equals method (case (Full(x), y) => x == y). But
doing so will probably break lots of existing code :), so it may not
be the way to go.

I'm not sure what best to do with this, as it looks like a design
decision to have this special case in the equals method, but at the
very least I thought I should mention it here.


Regards,
Jan

David Pollak

unread,
Mar 1, 2012, 4:55:38 PM3/1/12
to lif...@googlegroups.com
Back 4 years ago when I wrote Box, Scala took == a lot less seriously and played fast and loose with non-symmetrical ==.  I'm not sure I made a formal design decision, as much as a "hey, this is easy and as long as I'm here..."  And as you point out changing things now would likely be a silent breaking change, and thus would be anti-desirable.

Perhaps we should add java.lang.Comparable to Box: http://docs.oracle.com/javase/6/docs/api/  That'd give us a nice way to truly compare items.  What do you think?

Thanks,

David


 

Regards,
Jan

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net


Jan Ouwens

unread,
Mar 2, 2012, 11:56:48 AM3/2/12
to lif...@googlegroups.com
Hi David,

Thanks for your answer. I appreciate the historical perspective!

I'm not sure if adding Comparable would really help, since you would be imposing an ordering that's not really there. (I mean, is an Empty box "more than" or "less than" a Failure box?) It might make sense if the object it contains is also comparable, but I doubt if it's possible to make that work without losing symmetry again. And I'm not sure what the semantics would be if the contained object isn't comparable.

This sounds like a typical thing to add to the long list of nice-to-haves for the next big backward-compatibility-breaking rewrite version of 20X6. Although in the short term, I think it would help to simply make it a little more explicit in the ScalaDoc. It already says "Determines equality based upon the contents of this Box instead of the box itself." Maybe it can be expanded into something like "Determines equality based upon the contents of this Box instead of the box itself; as a result, it is not symmetric. Take care when mixing Boxes and their values in collections." Or something to that effect.


Regards,
Jan

Jan Ouwens

unread,
Mar 2, 2012, 7:23:21 AM3/2/12
to Lift
Hi David,

Thanks for your answer. I appreciate the historical perspective!

I'm not sure if adding Comparable would really help, since you would
be imposing an ordering that's not really there. (I mean, is an Empty
box "more than" or "less than" a Failure box?) It might make sense if
the object it contains is also comparable, but I doubt if it's
possible to make that work without losing symmetry again. I'm not sure
what the semantics would be if the contained object isn't comparable.

This sounds like a typical thing to add to the long list of nice-to-
haves for the next big backward-compatibility-breaking rewrite version
of 20X6. Although I think it would help to simply make it a little
more explicit in the ScalaDoc. It already says "Determines equality
based upon the contents of this Box instead of the box itself." Maybe
it can be expanded into something like "Determines equality based upon
the contents of this Box instead of the box itself; as a result, it is
not symmetric. Take care when mixing Boxes and their values in
collections." Or something to that effect.


Regards,
Jan


On Mar 1, 10:55 pm, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> Visi.Pro, Cloud Computing for the Rest of Ushttp://visi.pro
> Lift, the simply functional web frameworkhttp://liftweb.net

David Pollak

unread,
Mar 2, 2012, 12:28:51 PM3/2/12
to lif...@googlegroups.com
Please open a ticket (http://ticket.liftweb.net) and I'll update the documentation.

Thanks!

Jan Ouwens

unread,
Mar 4, 2012, 1:54:30 PM3/4/12
to lif...@googlegroups.com
OK, I will. Thanks!


Jan
Reply all
Reply to author
Forward
0 new messages