The following codes do the same thing:
module A
x = 3
end
@show A.x # 3
and
module A
x = 3
end
import .A
@show A.x # 3
Does import .A
do anything at all in the second example? If not, why is it allowed?
The following codes do the same thing:
module A
x = 3
end
@show A.x # 3
and
module A
x = 3
end
import .A
@show A.x # 3
Does import .A
do anything at all in the second example? If not, why is it allowed?
No, I think it does not nothing, as it is bringing to the scope a name that is already in the current scope.
I’m not sure if that is needed in any situation, maybe it is allowed because not allowing it would require a more complicated implementation of import in general. Do you see any important pitfall on allowing it?
Reviving an old topic.
module MySubModule
f()::Nothing = println("MySubModule.f(): Some code")
end # MySubModule
import .MySubModule # MySubModule defined in scope
MySubModule.f()
This works with and without the import
. I think, just as stated above, that MySubModule is already defined in scope, so bringing it in scope with import
again is a no-operation.
Surprisingly, I found exactly this in the documentation:
In contrast,
julia> import .NiceStuff
brings only the module name into scope.
Is this documentation from an era in which import NiceStuff
made sense? Or should the documentation be updated?
I suspect it’s just for consistency with the ongoing example. NiceStuff
is an ad-hoc module defined interactively — tutorial-like — and so it is definitionally not a package that needs to be brought into scope. That could indeed be noted! Maybe something like this:
… brings only the module name into scope. In this case, the
NiceStuff
module was interactively defined and so its name is already in scope, so this doesn’t do anything here.
But if it is not defined interactively, say in a script, the same scope considerations hold, and also there the import .SubModule
can be omitted. Does Julia have any trickery to show a list of identifiers that are in scope, I wonder now. If such a submodule is on the list, we would know a little bit more.
At any top-level, you can see this with names
:
julia> module NiceStuff
end
Main.NiceStuff
julia> names(@__MODULE__)
5-element Vector{Symbol}:
:Base
:Core
:InteractiveUtils
:Main
:NiceStuff