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

Error with optional argument in anonymous function defined in macro #36272

Closed
zsunberg opened this issue Jun 13, 2020 · 2 comments · Fixed by #36273
Closed

Error with optional argument in anonymous function defined in macro #36272

zsunberg opened this issue Jun 13, 2020 · 2 comments · Fixed by #36273

Comments

@zsunberg
Copy link
Contributor

In Julia 1.5.0 beta 1, the following fails:

macro m()
    :((a, b=1) -> a*b)
end
@m()(1)

with

ERROR: UndefVarError: b not defined
Stacktrace:
 [1] (::var"#1#2")(::Int64, ::Int64) at ./REPL[1]:3 (repeats 2 times)
 [2] top-level scope at REPL[1]:3

The reason I want to do this is here: https://github.com/JuliaPOMDP/POMDPs.jl/blob/609fe87094d4b1b01993deb0514565450266547f/src/generative.jl#L189. A workaround is to name the function.

@zsunberg
Copy link
Contributor Author

zsunberg commented Jun 13, 2020

Success/failure in julia versions:

  • 1.0.3: success
  • 1.1.0: success
  • 1.2.0: fail
  • 1.4.1: fail

@simeonschaub
Copy link
Member

That's definitely a bug with macro hygiene:

julia> @macroexpand @m
:((var"#14#a", b = 1)->begin
          #= REPL[1]:2 =#
          var"#14#a" * Main.b
      end)

Seems like hygiene is not aware of the optional argument b and so the b in the function body is interpreted as a global.

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

Successfully merging a pull request may close this issue.

2 participants