Skip to content

Commit

Permalink
Fix variable_ref_type for unsupported types and GenericNonlinearExpr (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 2, 2023
1 parent 908275f commit 0cd6a03
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Alias for `GenericNonlinearExpr{VariableRef}`, the specific
"""
const NonlinearExpr = GenericNonlinearExpr{VariableRef}

variable_ref_type(::GenericNonlinearExpr{V}) where {V} = V
variable_ref_type(::Type{GenericNonlinearExpr{V}}) where {V} = V

const _PREFIX_OPERATORS =
(:+, :-, :*, :/, :^, :||, :&&, :>, :<, :(<=), :(>=), :(==))
Expand Down
8 changes: 8 additions & 0 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ variable type associated with the model or expression type `F`.
"""
variable_ref_type(::F) where {F} = variable_ref_type(F)

function variable_ref_type(::Type{F}) where {F}
return error(
"Unable to compute the `variable_ref_type` of the type `$F`. If you " *
"are developing a JuMP extension, define a new method for " *
"`JuMP.variable_ref_type(::Type{$F})`",
)
end

variable_ref_type(::Type{V}) where {V<:AbstractVariableRef} = V

value_type(::Type{<:AbstractVariableRef}) = Float64
Expand Down
7 changes: 7 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -971,4 +971,11 @@ function test_operator_max()
return
end

function test_variable_ref_type()
for V in (GenericVariableRef{Int}, VariableRef)
@test variable_ref_type(GenericNonlinearExpr{V}) == V
end
return
end

end # module
14 changes: 14 additions & 0 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1555,4 +1555,18 @@ function test_parameter_arrays()
return
end

function test_variable_ref_type_unsupported()
for F in (Vector{VariableRef}, Vector{Int})
@test_throws(
ErrorException(
"Unable to compute the `variable_ref_type` of the type `$F`. If you " *
"are developing a JuMP extension, define a new method for " *
"`JuMP.variable_ref_type(::Type{$F})`",
),
variable_ref_type(F),
)
end
return
end

end # module TestVariable

0 comments on commit 0cd6a03

Please sign in to comment.