FCo
FCo

Reputation: 53

'UndefVarError: x not defined' when calling a macro outside of its module (Julia v1.1)

I'm trying to code a macro which calls a function defined within the same module (with Julia v1.1). I always get the ERROR: LoadError: UndefVarError: myModel not defined.

The code of the module is the following (Models.jl):

module Models

export Model
export createmodel
export add_variable
export @add_variable

mutable struct Model
    variable_symbols::Dict{String, Symbol}
    variable_data::Dict{Symbol, Array{Int64, 1}}
    Model(; variable_symbols = Dict([]), variable_data = Dict([])) = 
        new(variable_symbols, variable_data)
    Model(variable_symbols, variable_data) = new(variable_symbols, 
        variable_data)
end

function createmodel()
    model = Model()
    return model
end

function add_variable(model, label::String, value::Array{Int64, 1})
    symbol = Symbol(label)
    model.variable_symbols[label] = Symbol(symbol)
    model.variable_data[symbol] = value
end

macro add_variable(model, var)
    quote
        add_variable($model, $(string(var)), $var)
    end
end

end

The macro is called as following:

include("Models.jl")
using .Models

x = [1, 2, 3]

myModel = createmodel()
@add_variable(myModel, x)
show(myModel)

I know that this is an escaping/hygiene problem, but after trying many ideas I don't get the expected result. Up to now, my only way to get the expected result was to define the macro outside of the module to get: Model(Dict("x"=>:x), Dict(:x=>[1, 2, 3]))

Upvotes: 5

Views: 842

Answers (1)

mbauman
mbauman

Reputation: 31362

You just need to escape the variables that get passed into the macro:

macro add_variable(model, var)
    quote
        add_variable($(esc(model)), $(string(var)), $(esc(var)))
    end
end

All names in a macro are resolved in the context of the module its defined in unless you esc them — with an esc they're resolved in the context of the caller. You almost always want to esc the arguments to the macro. Names like add_variable, though, you want to resolve in the place where the macro itself was defined.

Upvotes: 5

Related Questions