-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Scoping bug with anonymous functions? #16727
Comments
versioninfo(): |
Of note, this only happens when running the script on a single thread:
|
I'm not able to reproduce this exactly as you describe; changing If you modify a local variable inside a function passed to |
But isn't this a bug if it violates scoping? This can lead to some very nasty aliasing bugs. I can also observe this behavior if I change pmap to map. Just to clarify I do not want to modify |
I see. Well, this is just how lexical scope works (as in scheme for example). You can use |
I guess I hadn't fully digested all the rules here: http://docs.julialang.org/en/release-0.4/manual/variables-and-scoping/#hard-vs-soft-local-scope. So the solution is to use |
Yes. To be fair, you're certainly not the first to bring this up. Actually wanting to modify parent-scope variables in an inner function is pretty rare, so it's quite possible that shouldn't be the default. |
I don't know, access to parent-scope variables is pretty much the whole reason to define an inner function. |
In my actual use case, Also, the discrepancy in behavior when running my code on one thread versus two feels sketchy. |
My understanding is that in 0.5 inner functions don't carry a performance penalty anymore. If so, then this style of coding might become more common. One gotcha is that you cannot just copy a function from global scope to a local one without checking that there are no variable clashes. |
Well, is that so different than saying you can't copy arbitrary code blocks between scopes without worrying about clashes? |
You're right, one has to be careful when copying code around. With functions though, one needs look out for different clashes if one copies a function from a global to a global scope than if one copies from a global to a local scope. I would be in favor of functions always having hard-scope, irrespective of whether a variable is local or global, and adding a |
I don't think we're going to change how lexical scope works at this point. |
Prints
-Inf
, while changingg
tof
in thetest3
call cause it to print"test"
which is correct.The text was updated successfully, but these errors were encountered: