Syntax for import from parent module

consider this MWE:

module Foo

macro id(expr)
    expr
end

module Bar

import Foo: @id       # how to refer to parent here without naming it?

@id(1+1)

end

end

Is it possible to import a symbol from the parent module without referring to that module by name? Eg with some syntax that involves .. or similar.

My use case: use the same function in submodules.

A workaround is putting these in a module, eg

module Foo

module Util

macro id(expr)
    expr
end

end

module Bar

import ..Util: @id       # how to refer to parent here without naming it?

@id(1+1)

end

end
1 Like
import ..@id
6 Likes

Sorry to revive an old topic.

Is there something one can do to import a function from the parent of the parent module? E.g.:

module A
  f(x) = 2
  module B
    module C
      # I want to call f here
    end # module C
  end # module B
end # module A

The following works:

module A
  f(x) = 2
  module B
    import ..f
    module C
      import ..f
      f(1)
    end # module C
  end # module B
end # module A

So I have to import f into B first, and then into C.

Is there a better way?

2 Likes

MOAR DOTS

julia> module A
         f(x) = 2
         module B
           module C
             import ...f
             f(1)
           end # module C
         end # module B
       end # module A
Main.A

julia> A.B.C.f(1)
2
9 Likes

What about using instead of import? How can I refer to the parent module in using?

1 Like

Same way as for import. Please do read the manual, all of this is documented. EDIT: sorry, I misunderstood the question. AFAIK it is not supported at the moment.

Is it?

julia> module A
         f(x) = 2
         module B
           module C
             using ...f
             f(1)
           end # module C
         end # module B
       end # module A
ERROR: invalid using path: "f" does not name a module
3 Likes

I think that’s unfortunately

https://github.com/JuliaLang/julia/issues/29131#issuecomment-420525223

1 Like