Skip to content

Commit

Permalink
Use let model=model in variable macro to improve type stability
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Sep 13, 2023
1 parent ca73f21 commit 34952ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3016,7 +3016,13 @@ macro variable(args...)
requested_container,
)
end

# Wrap the entire code block in a let statement to make the model act as
# a type stable local variable.
creation_code = quote
let $model = $model
$creation_code
end
end
if anonvar
# Anonymous variable, no need to register it in the model-level
# dictionary nor to assign it to a variable in the user scope.
Expand Down
6 changes: 6 additions & 0 deletions test/test_hygiene.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ Test.@test ex[3] == 6
Test.@test i == 10
Test.@test j == 10

# Test that `model` is inferred correctly inside macros, despite being a
# non-const global.
model = JuMP.Model()
JuMP.@variable(model, x[1:0])
Test.@test x == JuMP.VariableRef[]

end

0 comments on commit 34952ab

Please sign in to comment.