Skip to content

UndefVarError when using @eval in user-defined function #159

@ForceBru

Description

@ForceBru

Even though the documentation says that @eval works inside of user-defined functions, here's an example where it doesn't:

julia> import Pkg; Pkg.activate(temp=true); Pkg.add(name="TidierData", version="0.17.0")

julia> using TidierData

julia> df1 = DataFrame(trainsz=rand([200, 140, 110, 80], 100), loss=rand(100), p=rand([1,2,3], 100));

julia> df2 = DataFrame(trainsz=rand([200, 140, 110, 80], 100), loss=rand(100), p=rand([1,2,3], 100));

julia> function plot_comparison(losses_ARp, what)
               for (i, trainsz) in enumerate([200, 140, 110, 80])
                       df_ARp = @eval @filter(losses_ARp, trainsz == $trainsz)
                       df_ARp_comp = @eval @filter(what, trainsz == $trainsz)
               end
       end;

julia> plot_comparison(df1, df2)
ERROR: UndefVarError: `losses_ARp` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/TidierData/59JUA/src/TidierData.jl:422 [inlined]
 [2] top-level scope
   @ REPL[5]:415
 [3] eval
   @ ./boot.jl:430 [inlined]
 [4] plot_comparison(losses_ARp::DataFrame, what::DataFrame)
   @ Main ./REPL[5]:3
 [5] top-level scope
   @ REPL[6]:1

Are there any workarounds?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions