Skip to content

Commit

Permalink
Add nice fallback for build_variable (#2451)
Browse files Browse the repository at this point in the history
* Add nice fallback for build_variable

* Forgot to hit save before committing...

* Actually run the tests
  • Loading branch information
odow authored Feb 13, 2021
1 parent ced0fe8 commit c432f66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -998,16 +998,37 @@ _esc_non_constant(x) = esc(x)
# Example: `@variable m [1:3] foo` will allocate an vector of element type `variable_type(m, foo)`
# Note: it needs to be implemented by all `AbstractModel`s
variable_type(m::Model) = VariableRef
# Returns a new variable. Additional positional arguments can be used to dispatch the call to a different method.
# The return type should only depends on the positional arguments for `variable_type` to make sense. See the @variable macro doc for more details.
# Example: `@variable m x` foo will call `build_variable(_error, info, foo)`

"""
build_variable(_error::Function, info::VariableInfo; extra_kw_args...)
Returns a new variable.
Extensions should define a method with additional positional arguments to
dispatch the call to a different method. The return type should only depend on
the positional arguments for `variable_type` to make sense.
As an example, `@variable(model, x, foo)` foo will call
`build_variable(_error, info, foo)`
See the [`@variable`](@ref) macro doc for more details.
"""
function build_variable(_error::Function, info::VariableInfo; extra_kw_args...)
for (kwarg, _) in extra_kw_args
_error("Unrecognized keyword argument $kwarg")
end
return ScalarVariable(info)
end

function build_variable(_error::Function, ::VariableInfo, args...; kwargs...)
_error(
"Unrecognized arguments: $(join(args, ", ")). (You may have passed " *
"these as positional arguments, or as a keyword value to " *
"`variable_type`.)\n\nIf you're trying to create a JuMP extension, " *
"you need to implement `build_variable`."
)
end

function build_variable(_error::Function, variable::ScalarVariable,
set::MOI.AbstractScalarSet)
return VariableConstrainedOnCreation(variable, set)
Expand Down
12 changes: 12 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,18 @@ end
@variable(model, x)
@test num_variables(model) == 2
end

@testset "unrecognized_variable_type" begin
model = Model()
err = ErrorException(
"In `@variable(model, x, 2, variable_type = 1)`: Unrecognized " *
"arguments: 2, 1. (You may have passed these as positional " *
"arguments, or as a keyword value to `variable_type`.)\n\nIf " *
"you're trying to create a JuMP extension, you need to implement " *
"`build_variable`."
)
@test_throws_strip err @variable(model, x, 2, variable_type = 1)
end
end

@testset "Macros for JuMPExtension.MyModel" begin
Expand Down

0 comments on commit c432f66

Please sign in to comment.