News for hackers. Stuff that matters.
Re:JS 2 CoffeeScript Compiler by fooyc on Wednesday September 14, 2011 @09:59AM (#2996260)
It fails to return nothing here:

e.onclick = function() { false; } // returns nothing

Translates to:

e.onclick = -> false // returns false

Re:JS 2 CoffeeScript Compiler by walrus on Wednesday September 14, 2011 @01:14PM (#2997026)
I don't understand why this is being downvoted. The compiler's behavior is definitely wrong here, as you showed with your example. This issue could even come up in practice. Suppose you have a checkbox with the id 'cb' in your DOM. Consider the following script:

    var list = [true, false, true, true, false];
    document.getElementById('cb').onclick = function() {list.pop();};
When the checkbox is clicked, it should remove the last item in the list, then put a checkmark into the box. Run this through the Js2coffee compiler and you will get

    list = [ true, false, true, true, false ]
    document.getElementById("cb").onclick = -> list.pop()
Run this back through the CoffeeScript compiler to get: (modified slightly for brevity)

    var list = [true, false, true, true, false];
    document.getElementById("cb").onclick = function() {return list.pop();};
Notice that it now returns the result of list.pop(). Since the last item in the list is false, this will prevent the default browser behavior, so the checkbox will not be checked when it is clicked the first time.
Re:JS 2 CoffeeScript Compiler by rgbrgb on Wednesday September 14, 2011 @10:46AM (#2996440)
What's the purpose of the 'false;' statement? I believe CoffeeScript always returns the last line of a function.
Re:JS 2 CoffeeScript Compiler by fooyc on Wednesday September 14, 2011 @11:56AM (#2996711)
An example.

This could be any statement evaluating to false.

Coffee script returns the result of the last statement. Javascript don't. So, failing to take this into account produces coffeescript that do not have the same behavior than the original javascript.

This is particularly important for iterators that return false for stopping iteration. Or for DOM event handlers that return false for stopping the propagation of the event.

Re:JS 2 CoffeeScript Compiler by rgbrgb on Wednesday September 14, 2011 @12:55PM (#2996952)
While I totally agree with sid0 that it would be nice for 'academic' reasons if the compiler translated js->cs perfectly, I guess I'm having trouble understanding where this would actually come up as a problem. In my opinion, js functions should always return a value (even if it's just 'this'). Maybe a real code example of where your program would fail because of this?
Re:JS 2 CoffeeScript Compiler by fooyc on Wednesday September 14, 2011 @01:08PM (#2997003)
> would be nice for 'academic' reasons if the compiler translated js->cs perfectly

You should expect this from a compiler, actually

> In my opinion, js functions should always return a value

Sounds wrong

> real code example of where your program would fail because of this?

Simple:

    // That's perfectly fine; why would I return anything from this ?
    link.onclick = function() {
        doSomething();
    };

    // Now, translated in coffeescript:
    link.onclick = ->
        return doSomething(); // if this returns false, the "click" event is canceled
Re:JS 2 CoffeeScript Compiler by fooyc on Wednesday September 14, 2011 @03:40PM (#2997564)
An other good example is constructors; say you have a function like this:

    function MyClass(x){
        this.x = x;
    }
In Coffeescript this translates to

    MyClass = (x) ->
        @x = x
`x` is returned; and if you return something from a constructor, something is returned instead of the constructed object (`new MyClass({})` returns `{}`).
Re:JS 2 CoffeeScript Compiler by lmkg on Wednesday September 14, 2011 @02:26PM (#2997327)
> In my opinion, js functions should always return a value (even if it's just 'this').

Yeah, well, you know, that's just, like, your opinion, man.

It's perfectly valid for JavaScript functions to not return something. Your opinion about always returning something (which I agree with, btw) is only a personal stylistic preference, and from the code I've seen in the wild, it's not really that widespread.

As fooyc points out, the behavior of onClick events in the DOM changes depending on what return value is received. As I understand it, when an onClick executes a script, the truthiness of the value it returns determines whether the event continues to bubble up the DOM after the onClick has executed. At a guess, 75% of the onClick handler functions I've seen in the wild do not return a value; whether they realize it or not, all of those handlers implicitly rely on the appropriate truthiness of not returning a value for the rest of the page to behave correctly. Suddenly giving those functions a return value, especially one the developer didn't intentionally choose and didn't expect to have any effect, could cause quite a bit of unexpected behavior.

This also seems like an easy issue to address. No return statement in the JavaScript? Add an empty return in the CoffeeScript.

Re:JS 2 CoffeeScript Compiler by sid0 on Wednesday September 14, 2011 @11:29AM (#2996589)
The purpose of the false statement is irrelevant. I'm generally not a big fan of perfection in programming, but compilers are one instance it's worth aiming for.
Re:JS 2 CoffeeScript Compiler by skeptical on Wednesday September 14, 2011 @10:15AM (#2996335)
Just out of curiosity, how does one returns nothing in coffeescript?
Re:JS 2 CoffeeScript Compiler by endtwist on Wednesday September 14, 2011 @10:35AM (#2996402)

    fn = -> return