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

Inconsistencies in @closure semantics #11

Open
c42f opened this issue Feb 18, 2019 · 6 comments
Open

Inconsistencies in @closure semantics #11

c42f opened this issue Feb 18, 2019 · 6 comments

Comments

@c42f
Copy link
Owner

c42f commented Feb 18, 2019

The semantics of variable capture in @closure were originally intended to be the same as julia, but I got this wrong because at the time I didn't understand the intended semantics of the core language which is that the value associated with a name may be changed either within the closure or within the surrounding function and in both of these cases the change must be dynamically reflected in the other scope.

In particular, the value of x in the following is modified outside the closure, followed by returning closure, and there's nothing @closure can know about this:

using FastClosures

function foo()
    x = 10
    c = ()->x
    x = 20
    c()
end

function bar()
    x = 10
    c = @closure ()->x
    x = 20
    c()
end

julia> foo()
20

julia> bar() # @closure breaks this!
10

On the other hand, @closure does quite a bit of work to allow the following pattern to work the same as in the core language:

function baz()
    x = 10
    c = ()->(x = 20)
    c()
    x
end

function qux()
    x = 10
    c = @closure ()->(x = 20)
    c()
    x
end

julia> baz()

20

julia> qux()
20

(Though x+=10 doesn't appear to work... I guess that's a bug. Ugh!)

In hindsight I think this makes the semantics @closure pretty confusing and it would be better to just freeze all bindings inside the closure to fix them to the values they had at closure creation time. See also some discussion in #5 (CC @bramtayl)

Some possible names for such a macro...

@capturevalues # semantically questionable ?
@freeze_captures
@freeze
@rebind
@constbind # questionable
@newbindings
@let # too technical ?
@c42f
Copy link
Owner Author

c42f commented Feb 18, 2019

more possible names...

@localvars
@forcelocal

@bramtayl
Copy link

+1 for @freeze_captures

@bramtayl
Copy link

Actually just @freeze seems like it would be fine.

@c42f
Copy link
Owner Author

c42f commented Feb 19, 2019

@freeze is ok, though it's a bit too much of an analogy for me. I'd prefer something more literal, perhaps @rebind.

@c42f
Copy link
Owner Author

c42f commented Aug 13, 2019

Looking back, I can't say I'm entirely happy with any of the names above. @bind might be better?

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

No branches or pull requests

2 participants