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

Bug: weird behavior in name resolving because of the lack of "var" in compiled code #5023

Closed
whoiscc opened this issue Mar 23, 2018 · 1 comment
Labels

Comments

@whoiscc
Copy link

whoiscc commented Mar 23, 2018

test = ->
    dynamic_scoped_function = ->
        console.log("variable function value: #{variable_function()}")

    variable_function = ->
        "not in nested function"

    nested_function = ->
        variable_function = ->
            "in nested function"

        nested_caller = ->
            console.log("calling dynamic scoped function from nested caller...")
            dynamic_scoped_function()
        
        {nested_caller}

    {dynamic_scoped_function, nested_function}

# variable function value: not in nested function
test().dynamic_scoped_function()
# calling dynamic scoped function from nested caller...
# variable function value: in nested function
test().nested_function().nested_caller()

This behavior is awkward in my opinion, because when I was writing dynamic_scoped_function, I assumed that variable_function would always be resolved to the outer one. In the original version of my buggy code where I found this problem, the inner variable_function has nothing to do with dynamic_scoped_function. They just happen to have the same name.

This unexpected situation could be eliminated if I add var on both variable_function's definition. Could the compiler act in this way, too?

@vendethiel
Copy link
Collaborator

vendethiel commented Mar 23, 2018

This unexpected situation could be eliminated if I add var on both variable_function's definition. Could the compiler act in this way, too?

CoffeeScript uses implicit scope. If we did that, we'd make it impossible to mutate a variable.
There were discussion to have an assignment operator for outer scope (the fork livescript uses :=) though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants