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

proposal: chainable comparison operators #370

Closed
thejoshwolfe opened this issue May 10, 2017 · 3 comments
Closed

proposal: chainable comparison operators #370

thejoshwolfe opened this issue May 10, 2017 · 3 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.

Comments

@thejoshwolfe
Copy link
Sponsor Contributor

We certainly don't need this feature, but as I'm working on #114, I realized that it makes sense to be able to chain some comparison operators. C doesn't allow this, but we could do it and it would make perfect sense, so I'm writing this proposal to formally discuss the idea.

  • Chainable ==. a == b == c is equivalent to a == b and b == c (and b is evaluated only once).
  • Chainable < and <=. a OP b OP c is equivalent to a OP b and b OP c, where OP is one of < or <=.
  • Chainable > and >=. similar to above.

Python has this feature, but I'm proposing a more restrictive version than python's operator chaining. In python, you can do a != b != c, and I don't remember exactly what that does. That's a problem, so I'm proposing you can't do that in Zig. The proposal is that you can chain operators that work transitively, which is when a OP b and b OP c then it's obvious that a OP c. This is true for the above three cases, and isn't true of all any other mixture of comparison operators.

There's one subtle issue here, which is the hidden use of the short-circuit and operator. It may seem at first glance that a() == b() == c() would call all three functions when really it only needs to call the first two sometimes. That might be obvious to some readers, but might not be to all readers.

Fwiw, python has the hidden short circuit operator like I'm proposing.

@thejoshwolfe thejoshwolfe added enhancement Solving this issue will likely involve adding new logic or components to the codebase. lowpriority labels May 10, 2017
@andrewrk
Copy link
Member

I think this is one of those features that looks and sounds nice, but ultimately makes the language bigger, requires the programmer to remember more things, and isn't worth that cost.

a == b == c is barely more convenient than a == b and b == c, and plus I read that as (a == b) == c, which doesn't even make sense according to types, if a,b,c are integers. I don't find this feature worth it. I make a motion to reject this proposal.

@thejoshwolfe
Copy link
Sponsor Contributor Author

sounds good.

@tiehuis tiehuis added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. rejected labels Sep 15, 2017
@momumi
Copy link
Contributor

momumi commented Jan 11, 2020

At the moment its undecided whether zig switch statements should work with floating point values (#359, #3136). If we had this feature you could write expressions like this:

if (x < 0) {
} else if (0 <= x < 100) {
} else if (x >= 100) {
} else {
}

which I think is elegant and more intuitive then using a switch statement. You can do this now with compound and, but it makes the code harder to read. This change would be much simpler than the others suggested in (#359) so I think we should reconsider adding this feature.

I think this is one of those features that looks and sounds nice, but ultimately makes the language bigger, requires the programmer to remember more things, and isn't worth that cost.

This feature only needs a tiny change to the grammar of the language, and I think its worth it:

CompareOp := '<' | '<=' | '>' | '>=' | '==' | '!='

// New version
CompareExp := Exp (CompareOp Exp)+

// Old version
CompareExp_OLD := Exp CompareOp Exp

Of course there is the argument that it can be used to write confusing expressions, but in practice people won't unless they are deliberately trying to obfuscate their code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants