You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Generated by CoffeeScript 1.3.3(function(a){if(!(a!=null)){return1;}if(a==null){return2;}switch(false){casea==null:
return3;case!!(a!=null):
return4;}return0;});
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.
The text was updated successfully, but these errors were encountered:
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)return1;if(e==null)return2;switch(!1){casee==null:
return3;casee!=null:
return4;}return0;});}).call(this);
(BTW, that also "proves" that the original coffee output is equivalent to the simplified boolean forms you proposed =D)
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).
First of all, I did search Issues. I found current implementation of conditionals quite crappy when it comes to total negation.
One concrete example:
compiles to
I believe that all should compile into the simplified boolean expression form(i.e.
a==null
ora!=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)
anda==null
turns out different? I don't remember any, but either way!!(a!=null)
is meaningless as(a!=null)
is already boolean.The text was updated successfully, but these errors were encountered: