Using everything exported in a module and its submodules

I am currently replacing some String arguments of my mutable structure with dedicated types instead.

The following MWE works perfectly fine:

module MyPackage
  ### Some code
  module Options
  
      module SolveMod
          struct Both end  # public interface
          struct BranchBendersCut end  # public interface
          ## Some other options...
          const USolveMod = Union{Both,BranchBendersCut}  # not part of the public interface
  
          export USolveMod
      end
  
      module SPSolve
          struct Poly end  # public interface
          struct LP end  # public interface
          const USPSolve = Union{Poly,LP}  # not part of the public interface
          export USPSolve
      end
  
      export SolveMod, SPSolve
  end
  
  
  using .Options: SolveMod.USolveMod, SPSolve.USPSolve
  using Parameters
  @with_kw mutable struct OptimizeParameters @deftype String
     solve_mod::USolveMod
     sp_solve::USPSolve
  end
end

In my non MWE-case, I have 7 (out of 32) parameters for my solver that need a dedicated type, instead of two in the MWE (SolveMod and SPSolve). So I was wondering if I could write something like:

using .Options: All Instead of using .Options: SolveMod.USolveMod, SPSolve.USPSolve 3rdClass.Uniontype, 4thClass.Uniontype (etc.)

:smiley:

You could export all of them in options, and a user could import .Options in case they don’t want all of them.

(In principle one could even export via a for loop, but doing it explicitly might be clearer.)

Thank you for your reply :smiley: Am I not already exporting all of them?

cf. line:

export SolveMod, SPSolve, WriteResults # etc. other types

I am not sure I understand what you suggest :sweat_smile: I have modified my MWE, because I forgot to mention that module Options is within module MyPackage.

Also, I tried to use:
import .Options
instead of
using .Options: SolveMod.USolveMod, SPSolve.USPSolve as suggested in your reply but then USolveMod becomes undefined.

ERROR: LoadError: UndefVarError: `USolveMod` not defined

Anyway, I just remarked that it is not so a big deal to write all the 7 modules by hand :laughing:

1 Like

Okay! I found it all :smiley: I indeed had to export everything

module MyPackage
  module Options
  
      module SolveMod
          struct Both end  # public interface
          struct BranchBendersCut end  # public interface
          struct ILP end  # public interface
          struct NoOptimize end  # public interface
          struct gF end  # public interface
          struct gFexploreonlyILP end  # public interface
          struct gFexploreonlyben end  # public interface
          const USolveMod = Union{Both,BranchBendersCut,ILP,NoOptimize,gF,gFexploreonlyILP,gFexploreonlyben}  # not part of the public interface
  
          export Both, BranchBendersCut, ILP, NoOptimize, gF, gFexploreonlyILP, gFexploreonlyben, USolveMod
      end
  
      using .SolveMod
  
      module SPSolve
          struct Poly end  # public interface
          struct LP end  # public interface
          const USPSolve = Union{Poly,LP}  # not part of the public interface
          export Poly, LP, USPSolve
      end
  
      using .SPSolve
  
      module WResults
          struct WHTML end  # public interface
          struct WLocal end  # public interface
          const UWriteResults = Union{WHTML,WLocal,Bool}  # not part of the public interface
          export WHTML, WLocal, UWriteResults
      end
      using .WResults
  
      export SolveMod, SPSolve, WResults, Both, BranchBendersCut, ILP, NoOptimize, gF, gFexploreonlyILP, gFexploreonlyben, USolveMod, Poly, LP, USPSolve, WHTML, WLocal, UWriteResults
  end
end

using .Options

Now it works like a charm, thanks :slight_smile:

2 Likes

It looks like this could be a usecase for the Reexport package.

2 Likes