# Using MathOptInterface.AbstractOptimizer in parametric type declaration

**URL:** https://discourse.julialang.org/t/using-mathoptinterface-abstractoptimizer-in-parametric-type-declaration/124486
**Category:** Optimization (Mathematical)
**Tags:** jump, optimization
**Created:** [January 6, 2025, 11:29pm UTC](https://discourse.julialang.org/t/using-mathoptinterface-abstractoptimizer-in-parametric-type-declaration/124486 "2025-01-06T23:29:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![danrib07](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danrib07/32/212751_2.png) [@danrib07](https://discourse.julialang.org/u/danrib07)
#### Post date: [January 6, 2025, 11:29pm UTC](https://discourse.julialang.org/t/using-mathoptinterface-abstractoptimizer-in-parametric-type-declaration/124486/1 "2025-01-06T23:29:23Z")

</div>

Hi all,

I have a question regarding using the `MathOptInterface.AbstractOptimizer` for parametric type declaration when defining a composite type. For example, take the type definition below:

```julia
using JuMP # JuMP reexports MathOptInterface as MOI
using Ipopt

struct TestType{U <: MOI.AbstractOptimizer}
    test::U
end

my_test = TestType(Ipopt.Optimizer)

```

The idea here is that the user can pass a number of possible solvers to instantiate the composite type; however, the snippet above gives me the following error:

```julia
ERROR: MethodError: no method matching TestType(::Type{Ipopt.Optimizer})
The type `TestType` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
  TestType(::U) where U<:MathOptInterface.AbstractOptimizer
   @ Main c:\Users\...\minexample.jl:5

Stacktrace:
 [1] top-level scope
   @ c:\Users\...\minexample.jl:8

```

I thought that `U <: MOI.AbstractOptimizer` would be a valid type declaration because `Ipopt.Optimizer` is a subtype of `MOI.AbstractOptimizer`, for example:

```julia
julia> Ipopt.Optimizer <: MOI.AbstractOptimizer
true

```

What is the correct way to declare the type parameter for `U` in the struct `TestType`? Thank you in advance for the help!

---

<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: [January 6, 2025, 11:40pm UTC](https://discourse.julialang.org/t/using-mathoptinterface-abstractoptimizer-in-parametric-type-declaration/124486/2 "2025-01-06T23:40:06Z")

</div>

The issue is that `TestType(Ipopt.Optimizer)` is passing in the type of an optimizer, not an instance of it.

You either need `TestType(Ipopt.Optimizer())`, or you need something like:

```Julia
julia> struct TestType{U<:Real}
           test::Type{U}
       end

julia> TestType(Int)
TestType{Int64}(Int64)

julia> TestType(1)
ERROR: MethodError: no method matching TestType(::Int64)

Closest candidates are:
  TestType(::Type{U}) where U<:Real
   @ Main REPL[1]:2

Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

```

It might help your understanding to try examples with `Real` instead of `MOI.AbstractOptimizer`:

```Julia
julia> struct TestType{U<:Real}
           test::U
       end

julia> TestType(Int)
ERROR: MethodError: no method matching TestType(::Type{Int64})

Closest candidates are:
  TestType(::U) where U<:Real
   @ Main REPL[1]:2

Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> TestType(1)
TestType{Int64}(1)

```

However, from a JuMP perspective, you probably just want to make this:

```julia
struct TestType
    test::Any
end

```

This would support

```Julia
GRB_ENV = Gurobi.Env()
TestType(() -> Gurobi.Optimizer(GRB_ENV))

```

and

```julia
TestType(optimizer_with_attributes(Ipopt.Optimizer, "tol" => 1e-8))

```

JuMP’s optimizer constructors do to need to be `<:MOI.AbstractOptimizer`.
