# Using expression must be at top level

**URL:** <https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088>\
**Category:** New to Julia\
**Created:** [July 16, 2024, 7:56am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088 "2024-07-16T07:56:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 16, 2024, 7:56am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/1 "2024-07-16T07:56:54Z")

</div>

In order to make my package [Solver parametric](https://github.com/jkhamphousone/RingStarProblems.jl/issues/11), I am trying to read the user prompt to determine which solver he wants to use alongside with JuMP

```julia
mutable struct SolverOptimizer
    solver::DataType
    id::Int
end

function readsolveropt()
    solver = readline()
    println("Select your solver [Gurobi | SCIP]")

    if occursin("gurobi", lowercase(solver))
        using Gurobi
        return SolverOptimizer(Gurobi.Optimizer, 1)
    else
        using SCIP
        return SolverOptimizer(SCIP.Optimizer, 2)
    end
end

```

However, I get the error `ERROR: syntax: "using" expression not at top level`, both when used in a module and when used in the REPL. Does that mean I can’t encapsulate my if statement in a function and must put it in top level where my main module is defined?

Also, is it appropriate to create a post on Julia Discourse for this kind of “small” questions? Is there a more appropriate place, e.g., would you recommend that I create a Slack account or something more “chatty” “SMS” than a formal post?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 16, 2024, 8:04am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/2 "2024-07-16T08:04:17Z")

</div>

> [@K5Julien](#):
>
> Does that mean I can’t encapsulate my if statement in a function and must put it in top level where my main module is defined?

Yes. `using` can’t be used inside a function.

> [@K5Julien](#):
>
> is it appropriate to create a post on Julia Discourse for this kind of “small” questions?

Yes.

> [@K5Julien](#):
>
> trying to read the user prompt to determine which solver he wants to use alongside with JuMP

There is surely a better approach. Instead of taking a string like `"gurobi"` from the user, you could simply take the `MOI.AbstractOptimizer` (e.g., `Gurobi.Optimizer`).

Furthermore, if you really want to explicitly depend on specific solvers, you should prefer _weak_ dependencies (via package extensions) over the usual, _strong_, dependencies.

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 16, 2024, 8:16am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/3 "2024-07-16T08:16:12Z")

</div>

Dear @nsajko, thank you so much for your reply! I was not aware of weak dependencies! This is suitable in my case 😊

I still hesitate between coding via MathOptInterface or via JuMP and specific solvers.

Concorde.jl doesn’t rely on JuMP but I also read in MathOptInterface doc that creating your own solver is a lot of more work!

I need more experienced Julia developers points of view on this choice!

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 16, 2024, 8:19am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/4 "2024-07-16T08:19:35Z")

</div>

> [@K5Julien](#):
>
> I still hesitate between coding via MathOptInterface or via JuMP and specific solvers.

Personally I almost never used JuMP.jl, only MOI, but I think my advice above should apply independently of whether you use JuMP or MOI.

> [@K5Julien](#):
>
> MathOptInterface doc that creating your own solver is a lot of more work!

You don’t need to create your own solver, though.

---

<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:** [July 16, 2024, 8:22am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/5 "2024-07-16T08:22:18Z")

</div>

Instead of getting the user to pass a string, get them to pass the `Gurobi.Optimizer` object directly.

The user should call something like:

```julia
using SCIP
RSP.rspoptimize(pars, id_instance; optimizer = SCIP.Optimizer)

```

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 16, 2024, 8:27am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/6 "2024-07-16T08:27:46Z")

</div>

@odow @nsajko, thank you very much for your clear inputs 😃

---

<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:** [July 16, 2024, 8:31am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/7 "2024-07-16T08:31:18Z")

</div>

No worries 😄

A key design principle of JuMP is that it should be agnostic to the solver. The user could even use a solver that you have never seen or heard of and it should still work.

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 16, 2024, 8:35am UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/8 "2024-07-16T08:35:17Z")

</div>

I remember one of my PhD supervisor telling me at some point during my thesis:

“I can’t use CPLEX with your code, only Gurobi”

And I was thinking: “that is normal, I have not coded with the idea that you will!”  
And now I realize how in advance my supervisor was haha 😃

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 16, 2024, 2:51pm UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/9 "2024-07-16T14:51:33Z")

</div>

Is your solution documented somewhere in JuMP manual? If not, I believe it has its place there

---

<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:** [July 16, 2024, 8:33pm UTC](https://discourse.julialang.org/t/using-expression-must-be-at-top-level/117088/10 "2024-07-16T20:33:31Z")

</div>

See [Design patterns for larger models · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/design_patterns_for_larger_models/#Remove-solver-dependence,-add-error-checks)
