-
-
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
Explicit capture of variables in a closure. #14959
Comments
Related issue : JuliaLang/Distributed.jl#30 |
@amitmurthy: yes that issue is closely related: it is this very issue ;-) Which one did you mean? |
Edited above. |
@KristofferC: your examples suggest that you are concerned about functions in inner scopes. If so, making hard-scopes also hard for non-global variables could go some way. See #10559 When only "reading" variables, this could get quite verbose, would you need to write: |
I was concerned about accidentally modifying (rebinding) a variable local to the outer function from a closure so I wanted a syntax to only bring a set of chosen variables in scope for the closure. By default things can work exactly like now. |
I don't quite follow your comment, but maybe I can be clearer. If Julia had functions with hard scope also for non-global variables and had the function foo()
a = 1
b = 1
c = 1
# Only a and b is captured
function g(x)
nonlocal a = x
nonlocal b = x
c = x
end
g(5)
println(a)
println(b)
println(c)
end
foo()
# Prints
# 5
# 5
# 1 |
@mauro3 That would be a breaking change unless some ugly rule like "assume implicit Coming back to the Another option (whatever the syntax) would be to have all variables captured read-only ( |
I did not think about functions being first class objects so you would also need to capture those, would be annoying... An implicit const on all captured objects is in my view a good idea but I have no feeling of how often a captured variable is modified in Base / user code so I don't know the level of breakage it would do. |
Yes, that would be a breaking change. However, I think I would prefer it to the proposal here as function signatures are tricky enough already in Julia. Anyway, I just wanted to relate #10559 (closed) to this issue. Also, @JeffBezanson recently hinted that this hard-scope-only-affects-global-bindings could change: #16727 (comment) |
The current semi-hard scope rule for nested function is so dangerous and annoying. I really want @mauro3 's |
Right now when you create a closure you capture all the variables in the scope the closure is defined. This can lead to subtle bugs where you accidentally introduce a variable with the same name as one of the captured variables.
Would it be possible to introduce a syntax where the user explicitly lists what variables should be captured? A possible syntax of this (in spirit of lambda capture lists in c++) could be
which would capture
a
andb
. These both seem to be syntax error so should be available to use.An example of this in use:
Issues like #14948 seem to indicate that some sort of explicit capturing functionality could be useful to have.
The text was updated successfully, but these errors were encountered: