Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support equality operators with lists #606

Open
MageJohn opened this issue Sep 22, 2018 · 15 comments
Open

Support equality operators with lists #606

MageJohn opened this issue Sep 22, 2018 · 15 comments
Labels
api The VM's embedding API core The VM's core module — List, String, etc. future Issues that are closed but useful for future consideration vm The core Wren bytecode engine

Comments

@MageJohn
Copy link

Currently the code [1,2] == [1,2] returns false. I can't find anything in the documentation as to why this would be. Equality on lists should really be supported.

@mhermier
Copy link
Contributor

Because as this, it does object instance comparison. Code is missing for the support.

@minirop
Copy link
Contributor

minirop commented Sep 22, 2018

I'll add that the culprit is wrenValuesEqual in wren_value.c:
https://github.com/munificent/wren/blob/master/src/vm/wren_value.c#L1275-L1313

@MageJohn
Copy link
Author

Maybe if it can't test for equality it should raise an exception. It could lead to some difficult to find bugs if people assume that they can test the equality of two lists, but find that their code doesn't do what they expect; things shouldn't fail silently, unless we make it explicit.

@avivbeeri
Copy link
Contributor

I suppose it depends on the intended semantics, but the current behaviour doesn't bother me. Each list is a different object in the heap so I wouldn't consider them to be the same object, even if their contents is "equal".

I would expect the following to print true though.

var a = [1 ,2]
System.print(a == a)

To do as this issue suggests, equality operations would have to do deep inspection of the whole list contents (adding to the operational complexity). Would the same semantics extend to maps? What about deeply-nested lists?

@mhermier
Copy link
Contributor

mhermier commented Sep 23, 2018 via email

@ruby0x1
Copy link
Member

ruby0x1 commented Sep 23, 2018

I'm also not a fan of deep/expensive operations on syntax level.
As mentioned above, you also have to account for recursion, you can make a cycle trivially with lists and now == has undefined behaviour, or behaviour that is difficult to define with reasonable expectations.

There are functions though! We don't need to overload syntax for operations on values when functions are a unit of work.

List (or maybe even Sequence) can offer a function like equivalent or equals that does this, and allows more nuance and error handling. i.e list.equals(other).

@MageJohn
Copy link
Author

Isn't == an overloadable operator? The equality of two lists could be added as a method to the List class.

@mhermier
Copy link
Contributor

mhermier commented Sep 23, 2018 via email

@bjorn
Copy link
Contributor

bjorn commented Sep 23, 2018

Currently the code [1,2] == [1,2] returns false. I can't find anything in the documentation as to why this would be. Equality on lists should really be supported.

Are there any other scripting languages where a simple equality test of lists does a deep comparison? I think it's really uncommon at best, due to problems mentioned by @underscorediscovery.

Of course we know it from C++, but values are not pointers there unless written so explicitly, so it's clear when you do a pointer comparison or a deep comparison.

@mhermier
Copy link
Contributor

mhermier commented Sep 24, 2018 via email

@avivbeeri
Copy link
Contributor

Quick aside: It looks like the terms for what we are discussing are "physical" and "structural" equality. Wren's current == operator checks physical equality, as does the Object.same(_,_) static method.

An operator for testing structural equality might be useful, but it shouldn't replace the existing semantics of ==.

@mhermier
Copy link
Contributor

mhermier commented Sep 24, 2018 via email

@bjorn
Copy link
Contributor

bjorn commented Sep 24, 2018

I think most language does collection equality using the real equality operator.

It doesn't do this in Java, C#, Lua and JavaScript. Neither does it work like that in C++ or C if you're dealing with pointers (which is essentially what Wren object handles are).

I just found Python and Ruby do a deep comparison, but Python provides "is" for identify comparison and Ruby provides "equal?" for that. I think especially Ruby is pretty confusing here with their naming.

In any case I think it's not easy to say what "most languages" do, and by extension it's not easy to say what == is expected to do, since this would mostly depend on what the coder is familiar with.

@mhermier
Copy link
Contributor

mhermier commented Oct 4, 2018

It all depends on what you consider to be the source of equality. For me in java, .equals() seems to be for me a better source of equality than == since it can be overloaded.

If all is a question of having a physical and structural equality, for me === and == are the ideal candidates it is only a question to have them as operators. This relate to the fact that I'm a big fan of the <=> operator. That would make 3 operators of 3 symbols (===, !==, <=>).

@ruby0x1 ruby0x1 added api The VM's embedding API core The VM's core module — List, String, etc. vm The core Wren bytecode engine future Issues that are closed but useful for future consideration labels Sep 19, 2019
@buckle2000
Copy link

I feel like there is already is operator for reference equality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api The VM's embedding API core The VM's core module — List, String, etc. future Issues that are closed but useful for future consideration vm The core Wren bytecode engine
Projects
None yet
Development

No branches or pull requests

7 participants