# Dispatching on the type of a constraint or variable

**URL:** <https://discourse.julialang.org/t/dispatching-on-the-type-of-a-constraint-or-variable/36524>\
**Category:** Optimization (Mathematical)\
**Created:** [March 25, 2020, 10:20pm UTC](https://discourse.julialang.org/t/dispatching-on-the-type-of-a-constraint-or-variable/36524 "2020-03-25T22:20:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)\
**Post date:** [March 25, 2020, 10:20pm UTC](https://discourse.julialang.org/t/dispatching-on-the-type-of-a-constraint-or-variable/36524/1 "2020-03-25T22:20:57Z")

</div>

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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 25, 2020, 11:04pm UTC](https://discourse.julialang.org/t/dispatching-on-the-type-of-a-constraint-or-variable/36524/2 "2020-03-25T23:04:09Z")

</div>

If you just want to distinguish variables, use:

```nohighlight
f(x::VariableRef) = dual(LowerBoundRef(x))
f(x::ConstraintRef) = dual(x)

```

---

<div class="post-metadata">

**Author:** ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)\
**Post date:** [March 26, 2020, 6:51am UTC](https://discourse.julialang.org/t/dispatching-on-the-type-of-a-constraint-or-variable/36524/3 "2020-03-26T06:51:54Z")

</div>

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.
