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

Inconsistent conditionals: if !a?, unless a?, when a?, when !a? #2567

Closed
summivox opened this issue Oct 2, 2012 · 3 comments
Closed

Inconsistent conditionals: if !a?, unless a?, when a?, when !a? #2567

summivox opened this issue Oct 2, 2012 · 3 comments

Comments

@summivox
Copy link

summivox commented Oct 2, 2012

First of all, I did search Issues. I found current implementation of conditionals quite crappy when it comes to total negation.

One concrete example:

(a)->
    if !a? then return 1
    unless a? then return 2
    switch
        when a? then return 3
        when !a? then return 4
    return 0

compiles to

// Generated by CoffeeScript 1.3.3

(function(a) {
  if (!(a != null)) {
    return 1;
  }
  if (a == null) {
    return 2;
  }
  switch (false) {
    case a == null:
      return 3;
    case !!(a != null):
      return 4;
  }
  return 0;
});

I believe that all should compile into the simplified boolean expression form(i.e. a==null or a!=null). This is no C++ where operators might be overloaded and no longer form boolean algebra. As long as boolean algebra holds, nothing could prevent us to do these fundamental reductions.

Unless... someone name me a corner case where !(a!=null) and a==null turns out different? I don't remember any, but either way !!(a!=null) is meaningless as (a!=null) is already boolean.

@epidemian
Copy link
Contributor

I think this would be a nice addition to make the output simpler/more readable. For making it shorter, i think it doesn't make that much sense, as a JS minifier can take care of that:

echo '(a)->
  if !a? then return 1
  unless a? then return 2
  switch                 
    when a? then return 3
    when !a? then return 4
  return 0' | coffee -cs | uglifyjs -b

Output:

// Generated by CoffeeScript 1.3.3
(function() {
    (function(e) {
        if (e == null) return 1;
        if (e == null) return 2;
        switch (!1) {
          case e == null:
            return 3;
          case e != null:
            return 4;
        }
        return 0;
    });
}).call(this);

(BTW, that also "proves" that the original coffee output is equivalent to the simplified boolean forms you proposed =D)

@summivox
Copy link
Author

summivox commented Oct 2, 2012

Hmm. Then I remembered correctly. Yes readibility(and apparent stupidity ;) was one of the reasons, but it's more of a consistency problem, as this is just a particular case where unless boolean_expr compiles differently from if !(boolean_expr), especially when the expression gets more complex
I think there is a check in current implementation on whether negated boolean expressions can be simplified using identities, but apparently it's still quite flawed at the moment(see #2181 and #2506).

@summivox
Copy link
Author

summivox commented Oct 2, 2012

Oh I wasn't thorough. This was mentioned before:
#1393, #2298

But when !a? sure falls into the !!!expr===!expr category and should be fixed.

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

3 participants