esdiscuss/esdiscuss.org

Fix Left-Associative Compound Conditional Statements!!

benderTheCrime opened this issue · 2 comments

Hey there!

I was directed to this repository from this one

According to the README I should open a ticket here if I would like to look for a champion to support adding a feature to the spec.

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 (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:

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):

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

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:

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):

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:

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:

(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,

[] == [] === []

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.

Hi @benderTheCrime,

This is actually the repo for the source code of https://esdiscuss.org and therefore by "new feature" the readme means if you want a new feature added to the https://esdiscuss.org website. If you want a new feature added to the JavaScript language, you should go to https://mail.mozilla.org/listinfo/es-discuss to subscribe to the es-discuss mailing list, then you can e-mail your feature request to es-discuss@mozilla.org and it will appear as a new topic on https://esdiscuss.org/1 after a minute or two. It is generally recommended that you write your message in markdown, just as you would a GitHub issue.

Sorry for the confusion.

Ah, ok thanks! I will close this.