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

Inherited Class generated code does not match order of execution #1455

Closed
alanning opened this issue Jun 21, 2011 · 0 comments
Closed

Inherited Class generated code does not match order of execution #1455

alanning opened this issue Jun 21, 2011 · 0 comments

Comments

@alanning
Copy link

Severity Low

Proposal
Move the generated function declaration to the first line of the generated class.

Issue
Coffeescript currently outputs the __extends call prior to the function declaration of generated classes while order of execution is actually the opposite.

//Generated Code
NS.Snake = (function() {
  __extends(Snake, NS.Animal);
  function Snake() {
    Snake.__super__.constructor.apply(this, arguments);
  }
  //...
}
//Actual Execution Order:
NS.Snake = (function() {
  function Snake() {
    Snake.__super__.constructor.apply(this, arguments);
  }
  __extends(Snake, NS.Animal);
  //...
}

Rationale
While this currently causes no errors due to the happy coincidence that function declarations are always executed first, I believe implementing the proposed change avoids confusion for those trying to understand the generated code.

Example
An example of the issue can be found here: http://jsfiddle.net/alanning/yDnxL/

Reference
IRC #coffeescript channel discussion with jaskenas
Snipped for conciseness. Full chat log found here: http://irclogger.com/.coffeescript/2011-06-21#1308678991

<Adrian_>   I've got a couple suggested changes to coffeescript compiler behavior that I would like to get feedback on before I submit...
<Adrian_>   Class Extends behavior -> move "__extends(a,b)" call to after function declaration
<Adrian_>   The goal of the first change is to make it easier for beginners to understand the generated javascript as many tend to think (var func_name = function...) is the same as (function func_name...)
...
<jashkenas_work>    Adrian_: the __extends actually has to happen first, to make executable class bodies work right with static inheritance.
<Adrian_>   __extends can not happen before the function that it is extending is actually created
...
<jashkenas_work>    Adrian_: yes, but executable class bodies preserve the order of their statements.
<Adrian_>   it happens to work in how coffeescript generates it due to function _declarations_ always being executed first
<jashkenas_work>    and __extends has to happen before the first statement.
<jashkenas_work>    right, just so.
<Adrian_>   __extends does not actually happen first
<Adrian_>   if you put a breakpoint you will see it executing after the function declaration
<Adrian_>   i am suggesting changing it to match how the actual execution is occurring to help avoid confusion
...
<Adrian_>   jashkenas, to make what is happening in the class example clearer, add a namespace to the class names in the try coffeescript example
<Adrian_>   NS.Animal, etc
<Adrian_>   jashkenas_work, here is an example where you can see the difference: http://jsfiddle.net/alanning/yDnxL/
<Adrian_>   if you move the __extends call to before the function expression, the example stops working.
<Adrian_>   as you would expect
<Adrian_>   the fact that coffeescript outputs function declarations instead keeps the code working but it doesn't match order of execution
<jashkenas_work>    Adrian_: I understand the proposal.
alanning added a commit to alanning/coffee-script that referenced this issue Jul 20, 2011
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