Dispatching on the type of a constraint or variable

Hi,

How do I write a function signature such that I can dispatch on whether an argument is a variable or a constraint? I’d like to do this:

function f(x::TypeOfVariable) ... end
function f(x::TypeOfConstraint) ... end

The type of a constraint appears to be JuMP.ConstraintRef{JuMP.Model, MathOptInterface....} which seems a lot to type, and I’m guessing there must be an easier solution?

My use case is extracting shadow prices from constraints and variables (if it’s a variable, you need to call LowerBoundRef().

Thanks in advance!

If you just want to distinguish variables, use:

f(x::VariableRef) = dual(LowerBoundRef(x))
f(x::ConstraintRef) = dual(x)
1 Like

Thanks Oscar,

That’s embarrassingly obvious, and, actually, a really nice feature. As I recall (and it’s been a while) in C++ a Object<foo> and an Object<bar> have no connection in the type hierarchy.