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

add `@locals` macro for obtaining dictionary of local vars and values #29733

Merged
merged 2 commits into from Nov 30, 2018

Conversation

4 participants
@JeffBezanson
Copy link
Member

commented Oct 19, 2018

implements #29366

@StefanKarpinski

This comment has been minimized.

Copy link
Member

commented Oct 19, 2018

Wouldn't it make more sense for this to return a named tuple?

@JeffBezanson

This comment has been minimized.

Copy link
Member Author

commented Oct 19, 2018

I see this as just a reflection and debugging kind of thing, so a somewhat loosely-typed dictionary seems appropriate. The order of the variables is also going to be all over the place, so not having a well-defined order is a feature.

@dalum

This comment has been minimized.

Copy link
Contributor

commented Nov 5, 2018

Could this be useful in code generation as well? Something like:

function eval_with_local_vars(expr::Expr, x, y)
    @eval let $(dict_as_assignments(@locals))
        $expr
    end
end

which would now let expr contain the symbols x and y, for a suitable definition of dict_as_assignments.

@yuyichao

This comment has been minimized.

Copy link
Contributor

commented Nov 5, 2018

Could this be useful in code generation as well?

No.

@StefanKarpinski

This comment has been minimized.

Copy link
Member

commented Nov 5, 2018

Could this be useful in code generation as well?

No.

To expand on this a bit, in your example, the @eval is evaluated in global scope, so injecting local names doesn't have the effect you'd want. If you move the @eval outside of the function body then the eval has the effect you want but @locals is being called from a global context, not from the function body. So this is pretty much only useful for exposing what's in a local scope.

@dalum

This comment has been minimized.

Copy link
Contributor

commented Nov 6, 2018

I just checked out the branch and it actually does have the effect that I want, so maybe I didn't describe it properly. 😊 The point is that the thing inside the function body of f here easily translates into a macro that will work in all local scopes, without requiring the user to manually specify the dict of local variables. The example below is, of course, just a silly example:

julia> function f(expr, x, y)
           __locals__ = Base.@locals()
           ex = Expr(:let, Expr(:block), Expr(:block,
               :(__locals__ = $__locals__),
               (:($key = __locals__[$(QuoteNode(key))]) for (key) in keys(__locals__))...,
               :(__return__ = $expr),
               (:(__locals__[$(QuoteNode(key))] = $key) for (key) in keys(__locals__))...,
               :(__return__)))
           return eval(ex)
       end
f (generic function with 1 method)

julia> expr = :((a) -> a + x + y);

julia> h = f(expr, 1, 2)
#199 (generic function with 1 method)

julia> h(3)
6

If it was possible to write back to the @locals dict to modify the local scope, that would be really cool, though ...

@StefanKarpinski

This comment has been minimized.

Copy link
Member

commented Nov 6, 2018

If it was possible to write back to the @locals dict to modify the local scope

This may sound good but it ruins all ability to optimize anything.

@JeffBezanson JeffBezanson force-pushed the jb/locals branch from d31055e to c7f7422 Nov 28, 2018

@JeffBezanson JeffBezanson merged commit 67bba81 into master Nov 30, 2018

0 of 3 checks passed

continuous-integration/appveyor/pr Waiting for AppVeyor build to complete
Details
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
julia freebsd ci Build started
Details

@JeffBezanson JeffBezanson deleted the jb/locals branch Nov 30, 2018

fredrikekre added a commit that referenced this pull request Nov 30, 2018

fredrikekre added a commit that referenced this pull request Dec 1, 2018

fredrikekre added a commit that referenced this pull request Dec 1, 2018

fredrikekre added a commit that referenced this pull request Dec 3, 2018

fredrikekre added a commit that referenced this pull request Dec 4, 2018

fredrikekre added a commit that referenced this pull request Dec 4, 2018

fredrikekre added a commit that referenced this pull request Dec 5, 2018

Addition of NEWS and compat admonitions for important
changes between Julia 1.0 and 1.1, including:

- Custom .css-style for compat admonitions.

- Information about compat annotations to CONTRIBUTING.md.

- NEWS.md entry for PRs #30090, #30035, #30022, #29978,
  #29969, #29858, #29845, #29754, #29638, #29636, #29615,
  #29600, #29506, #29469, #29316, #29259, #29178, #29153,
  #29033, #28902, #28761, #28745, #28708, #28696, #29997,
  #28790, #29092, #29108, #29782

- Compat annotation for PRs #30090, #30013, #29978,
  #29890, #29858, #29827, #29754, #29679, #29636, #29623,
  #29600, #29440, #29316, #29259, #29178, #29157, #29153,
  #29033, #28902, #28878, #28761, #28708, #28156, #29733,
  #29670, #29997, #28790, #29092, #29108, #29782, #25278

- Documentation for broadcasting CartesianIndices (#30230).
- Documentation for Base.julia_cmd().
- Documentation for colon constructor of CartesianIndices (#29440).
- Documentation for ^(::Matrix, ::Number) and ^(::Number, ::Matrix).

- Run NEWS-update.jl.

Co-authored-by: Morten Piibeleht <morten.piibeleht@gmail.com>
Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>

fredrikekre added a commit that referenced this pull request Dec 5, 2018

Addition of NEWS and compat admonitions for important
changes between Julia 1.0 and 1.1, including:

- Custom .css-style for compat admonitions.

- Information about compat annotations to CONTRIBUTING.md.

- NEWS.md entry for PRs #30090, #30035, #30022, #29978,
  #29969, #29858, #29845, #29754, #29638, #29636, #29615,
  #29600, #29506, #29469, #29316, #29259, #29178, #29153,
  #29033, #28902, #28761, #28745, #28708, #28696, #29997,
  #28790, #29092, #29108, #29782

- Compat annotation for PRs #30090, #30013, #29978,
  #29890, #29858, #29827, #29754, #29679, #29636, #29623,
  #29600, #29440, #29316, #29259, #29178, #29157, #29153,
  #29033, #28902, #28878, #28761, #28708, #28156, #29733,
  #29670, #29997, #28790, #29092, #29108, #29782, #25278

- Documentation for broadcasting CartesianIndices (#30230).
- Documentation for Base.julia_cmd().
- Documentation for colon constructor of CartesianIndices (#29440).
- Documentation for ^(::Matrix, ::Number) and ^(::Number, ::Matrix).

- Run NEWS-update.jl.

Co-authored-by: Morten Piibeleht <morten.piibeleht@gmail.com>
Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>

fredrikekre added a commit that referenced this pull request Dec 5, 2018

Compat admonitions and NEWS for Julia 1.1 (#30230)
Addition of NEWS and compat admonitions for important changes between Julia 1.0 and 1.1, including:

- Custom .css-style for compat admonitions.

- Information about compat annotations to CONTRIBUTING.md.

- NEWS.md entry for PRs #30090, #30035, #30022, #29978,
  #29969, #29858, #29845, #29754, #29638, #29636, #29615,
  #29600, #29506, #29469, #29316, #29259, #29178, #29153,
  #29033, #28902, #28761, #28745, #28708, #28696, #29997,
  #28790, #29092, #29108, #29782

- Compat annotation for PRs #30090, #30013, #29978,
  #29890, #29858, #29827, #29754, #29679, #29636, #29623,
  #29600, #29440, #29316, #29259, #29178, #29157, #29153,
  #29033, #28902, #28878, #28761, #28708, #28156, #29733,
  #29670, #29997, #28790, #29092, #29108, #29782, #25278

- Documentation for broadcasting CartesianIndices (#30230).
- Documentation for Base.julia_cmd().
- Documentation for colon constructor of CartesianIndices (#29440).
- Documentation for ^(::Matrix, ::Number) and ^(::Number, ::Matrix).

- Run NEWS-update.jl.


Co-authored-by: Morten Piibeleht <morten.piibeleht@gmail.com>
Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.