ele...: I did not know about this sys.modules thing. Good to know.
Josh & ele...:
Agreed. I don't think pollution happens until the "using modX" call happens. So, I don't really understand why we can't have scope-level "using" commands.
And yes, maybe Julia *should* use a different keyword than "using" when applied to arbitrary scopes (as opposed to when we use it at the module-level).
...So linking back to Patrick's comment:
The "export" list sort of tells the module user what functions are meant to be used. It just happens that Julia's implementation of "using" pulls in all "export"-ed elements into the current module namespace. This works extremely well with the multi-dispatch engine.
However, I can also see a place for a slightly weaker directive: Let's call it "provide" for now. The "provide" directive could be used to tell the user what other elements (like constructors) are "supported" by the module. The "provide"-ed elements would not be pulled in by the "using" statement. Only "export"-ed values do that. Instead, "provide"-ed elements would *only* be pulled-in when a user calls "using >>namespace<< modX" (or some other keyword).
This would allow developers to use shorter names. Names that would otherwise collide if they were to be "export"-ed:
module Electroncis
type Circuit
name::String
elemlist::Array{CktElem}
...
end
abstract IndepCktElem
abstract drivePattern
type Vsrc <: IndepCktElem ...; end
type SineWave <: drivePattern ...; end
connect(c::Circuit, x::CktElem, n::NodeList) = ...
drive(c::Circuit, x::IndepCktElem, n::NodeList, p::drivePattern) = ...
...
export connect, drive #No problem: Can be handled by multi-dispatch
provide Circuit #Dangerous: Cannot always be resolved by multi-dispatch
provide Vsrc, SineWave #Also a little dangerous
end
Now, in the user's module, you can do the following:
using Electronics #Brings in connect & drive, as usual
#Eventually gets a little verbose when dealing with elements that cannot easily
#be resolved by multi-dispatch ("provide"-ed elements):
function make_circuit()
c = Electronics.Circuit("Myckt")
drive(c, Electronics.Vsrc(), [:SUPPLY, :GND], Electronics.SineWave(1e9))
return c
end
#But this function *could* be made more succinct:
function make_circuit2()
using namespace Electronics #Also brings in all other "legitimate" elements
c = Circuit("Myckt") #More succinct constructor call
#Things gets even nicer with more complex expressions:
drive(c, Vsrc(), [:SUPPLY, :GND], SineWave(1e9))
return c
end