Use constant variable from outer module in inner module

Hey community :slight_smile:

I’ve got a question about modules. I want to use a field of an outer module in an inner module. How would I do that?

module A
const A_const = "stuff"
    module B
        do_stuff(A_const)
    end
end
1 Like

Just figured it out :slight_smile:
Here is my solution.

module A
const A_const = "stuff"
    module B
        import ..A
        do_stuff(A.A_const)
    end
end
2 Likes

To be honest, this looks a bit fishy to me.

At some point you presumably want to have using .B in A. Then you get into circular references (A uses B and the other way around).

Usually one sees this the other way around, as in A uses B.

Of course I don’t know what you are trying to achieve, but perhaps a better alternative is

module A
  module C
    export x
    x = 1
  end
  using .C
  module B
    export y
    using ..C
    y = x + 1
  end
  using .B
  println(y)
end

Considering the bigger picture (again not knowing what your specific application looks like) I have found submodules to be more trouble than it’s worth for what I do. When modules get big enough to justify more structure, I prefer to make B into a separate package. But tastes vary.

1 Like

Just to give a different opinion, I have a package with about ten submodules, and I do not only use the innermost ones inside the outermost ones, but I also import names of the outermost modules to extend them inside the innermost ones. I never had any problem, nor considered stopping my use of submodules.

3 Likes