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

Allow tuples to be compared with == operator #748

Open
cburgdorf opened this issue Jun 22, 2022 · 3 comments
Open

Allow tuples to be compared with == operator #748

cburgdorf opened this issue Jun 22, 2022 · 3 comments

Comments

@cburgdorf
Copy link
Collaborator

cburgdorf commented Jun 22, 2022

What is wrong?

We currently can't easily compare tuples with the == operator. E.g. the following doesn't work.

if get_tuple() == (1, 2) {
...
}

Instead what we have to do is:

if get_tuple() == (1, 2) {
...
}
let (val1, val2): (u8, u8) = get_tuple()
if val1 == 1 and val2 == 2 {
}

How can it be fixed

We can probably tackle that at MIR lowering

Make PartialEq and Eq traits possible and use them for this.

@Y-Nak
Copy link
Member

Y-Nak commented Jun 22, 2022

We can probably tackle that at MIR lowering

To be honest, I don't think this is a good solution because mir lowering would be bloated if we continue to pack this kind of ad-hoc implementation into the phase. I think providing Eq or PartialEq trait would be the simplest and best solution.

@cburgdorf
Copy link
Collaborator Author

Yes, PartialEq trait would be my preferred solution as well. But in order for that to be possible we need to find out how to support generics / traits to work with all types and not just structs. That's actually one of the issues that if we crack them will enable a lot more sophisticated features in the language. Currently this is blocked because in the analyzer for a generic type T we can not figure out the location of the type and hence default it to memory for now. You mentioned you had some ideas how to get rid of the location specific code in the analyzer. I would appreciate your thoughts and ideas how to advance that.

@cburgdorf
Copy link
Collaborator Author

Now that we have support for Self it should be able to define PartialEq and then use that for == and !=. However, to be helpful with complex types such as tuples we will probably need to have macros and use them the way Rust does because otherwise people would have to implement PartialEq by hand for each specific tuple. But I believe the current rewrite lead by @Y-Nak already clears the way for such kind of macros.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants