Submodule structs as "default" generic names


#1

Here’s my current dilemma: I have a package/module M and within that, a submodule SA which exports structs AFoo and ABar. I would like, by default, the words M.Foo and M.Bar to refer to those two structs but I’d like other submodules (e.g., SB, which will export BFoo and BBar) to be able to override that default when SB is explicitly imported.

What’s the best way of doing this so that users don’t see warnings?


#2

You may assing a datatype to any global variable in the module, and as long as this global is not marked const, you can reassign it either with eval(M, expr) in that module, or with a setter function. On Julia 0.6, this is how i would start, (note the optional submodule does not export but rather actively override)

julia> workspace()
       module M

         setfoobar(foo, bar) = ( global Foo; global Bar; (Foo, Bar) = (foo, bar))

         module SA
           export AFoo, ABar  # not actually required for this to work
           struct AFoo end
           struct ABar end
         end

         Foo = SA.AFoo   # Default
         Bar = SA.ABar

       end

       M.Foo |> println
       M.Bar |> println

       # Consider this being imported on request

       module SB
         struct BFoo end
         struct BBar end
         using M.setfoobar
         __init__() = setfoobar(BFoo, BBar)
       end

       M.Foo |> println
       M.Bar |> println
       
M.SA.AFoo
M.SA.ABar
SB.BFoo
SB.BBar

Certainly, having non-const globals has some disadvantages.
I’m not convinced this is the best way of doing this, but it meets your specification.