Joseph Groseclose (2015-09-24T14:10:07.000Z)
forbes at lindesay.co.uk (2015-09-24T15:11:32.638Z)
## Left-Associative Compound Conditional Statements I would like the ability to compare many values in compound. As I do not see any real reason to avoid this, here is my proposal: Following [this document]( http://yehudakatz.com/2011/09/13/a-proposal-for-es-next-proposals/) (thanks @wykatz) ### What problem are we solving: Often, it makes it easier and more convenient to compare values in a range as opposed to comparing one to another and that value to even another. In order to avoid this, JavaScript could add the ability to "chain" many comparators, failing after the first inequality that is falsy. ### Provide Non-Trivial Example Code Showing the Problem: For the remainder of this issue/request, I will be using (almost exclusively) the "<" comparator, but any normal operators should work with this proposal, including but not limited to: * `==` * `===` * `>` * `<` * `> =` * `<=` * yada yada yada (not an operator) Two examples: ```javascript let foo = 1, bar = 2, baz = 3; if (foo < bar && bar < baz) { // ...do something! } ``` While vaguely trivial, this snippet demonstrates the easiest way to accurately compare these three values in a basic conditional. We have to compare either the first two values and the second to values serially or in parallel. Likewise (and slightly less trivial): ```javascript let arr = [ 'foo', 'bar', 'baz' ]; for (var i = 0; i < arr.length; ++i) { // do something... if (i > 1) { break; } } ``` The above illustrates an example where we want to iterate in a range. There are several solutions to this (including a compound condition in the `for` statement, cloning and slicing, or reversing the array and setting `i` greater than zero), but this is even less convenient than the original example! ### Show How the Example Code is Improved With the Proposal: Now, we only have two left-associative conditions to evaluate as opposed to three ```javascript let foo = 1, bar = 2, baz = 3; if (foo < bar < baz) { // ...do something! } ``` We have saved ourselves the trouble of including a good deal of logic in our simple `for` loop as well: ```javascript let arr = [ 'foo', 'bar', 'baz' ]; for (let i = 0, y = 2; i < arr.length < y; ++i) { // do something... } ``` What are Alternate ES3 (or ES5) Solutions to the Same Problem?: I've included some solutions to how this can be avoided, it is worth noting in this section that this is not so much about this situation being unavoidable so much as it is a ~~matter of convenience~~. Actually, scratch that, its not even JUST a matter of convenience. A simple evaluation in your developer console will tell you that this currently evaluates inconveniently (I am avoiding the term "incorrectly" here because it is not theoretically incorrect if we consider these evaluations left associative): ```javascript 1 < 2 < 3 // true where... true < 3 // true...because +true // 1 1 < 3 // true ``` Bear in mind that this is not a catch-all definition of this behavior: ```javascript false === false == false // false (wut?) ``` ### If the Proposal is For New Syntax, What if Anything Does it Desugar To? The desugaring is shown above, but the concatenation of conditionals can be converted into their non-concatenated equivalents: ```javascript (1 < 2 < 3) === (1< 2 & 2 < 3) // Not a typo ``` ### If New Syntax, Can it Be Compiled to ES3 (or ES5)? Absolutely, in the same way it has been outlined above ### Does the Proposal Change any Existing Semantics? I'm not entirely sure this is semantic (because the comparators of any two values should evaluate in the same way they always have), but it is worth noting in this section that any two values should work with compound conditionals, not just numbers. For example, ```javascript [] == [] === [] ``` should fail comparison. I realize this effectively makes `===` a silver bullet in compound conditional statements, but I can't see any other way this would work. Please let me know if there is any more context or detail I can provide, or whether there are considerations I have not made.
joe.groseclose at gmail.com (2015-09-24T14:30:02.367Z)
## Left-Associative Compound Conditional Statements I would like the ability to compare many values in compound. As I do not see any real reason to avoid this, here is my proposal: Following [this document]( http://yehudakatz.com/2011/09/13/a-proposal-for-es-next-proposals/) (thanks @wykatz) ### What problem are we solving: Often, it makes it easier and more convenient to compare values in a range as opposed to comparing one to another and that value to even another. In order to avoid this, JavaScript could add the ability to "chain" many comparators, failing after the first inequality that is falsy. ### Provide Non-Trivial Example Code Showing the Problem: For the remainder of this issue/request, I will be using (almost exclusively) the "<" comparator, but any normal operators should work with this proposal, including but not limited to: * == * === * > * < * > = * <= * yada yada yada (not an operator) Two examples: ```javascript let foo = 1, bar = 2, baz = 3; if (foo < bar && bar < baz) { // ...do something! } ``` While vaguely trivial, this snippet demonstrates the easiest way to accurately compare these three values in a basic conditional. We have to compare either the first two values and the second to values serially or in parallel. Likewise (and slightly less trivial): ```javascript let arr = [ 'foo', 'bar', 'baz' ]; for (var i = 0; i < arr.length; ++i) { // do something... if (i > 1) { break; } } ``` The above illustrates an example where we want to iterate in a range. There are several solutions to this (including a compound condition in the `for` statement, cloning and slicing, or reversing the array and setting `i` greater than zero), but this is even less convenient than the original example! ### Show How the Example Code is Improved With the Proposal: Now, we only have two left-associative conditions to evaluate as opposed to three ```javascript let foo = 1, bar = 2, baz = 3; if (foo < bar < baz) { // ...do something! } ``` We have saved ourselves the trouble of including a good deal of logic in our simple `for` loop as well: ```javascript let arr = [ 'foo', 'bar', 'baz' ]; for (let i = 0, y = 2; i < arr.length < y; ++i) { // do something... } ``` What are Alternate ES3 (or ES5) Solutions to the Same Problem?: I've included some solutions to how this can be avoided, it is worth noting in this section that this is not so much about this situation being unavoidable so much as it is a ~~matter of convenience~~. Actually, scratch that, its not even JUST a matter of convenience. A simple evaluation in your developer console will tell you that this currently evaluates inconveniently (I am avoiding the term "incorrectly" here because it is not theoretically incorrect if we consider these evaluations left associative): ```javascript 1 < 2 < 3 // true where... true < 3 // true...because +true // 1 1 < 3 // true ``` Bear in mind that this is not a catch-all definition of this behavior: ```javascript false === false == false // false (wut?) ``` ### If the Proposal is For New Syntax, What if Anything Does it Desugar To? The desugaring is shown above, but the concatenation of conditionals can be converted into their non-concatenated equivalents: ```javascript (1 < 2 < 3) === (1< 2 & 2 < 3) // Not a typo ``` ### If New Syntax, Can it Be Compiled to ES3 (or ES5)? Absolutely, in the same way it has been outlined above ### Does the Proposal Change any Existing Semantics? I'm not entirely sure this is semantic (because the comparators of any two values should evaluate in the same way they always have), but it is worth noting in this section that any two values should work with compound conditionals, not just numbers. For example, ```javascript [] == [] === [] ``` should fail comparison. I realize this effectively makes `===` a silver bullet in compound conditional statements, but I can't see any other way this would work. Please let me know if there is any more context or detail I can provide, or whether there are considerations I have not made.