In order to make my package Solver parametric, I am trying to read the user prompt to determine which solver he wants to use alongside with JuMP

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?

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.

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.