Would there be some disadvantage in using a module as a container of constants?
I sometimes have 5–10 related global constants, which I sometimes want to treat as a whole. I thought that the simplest solution was
params = (a = 3.14, b = "hello", c = 6.28)
# . . . use params.a, etc. . . .
So far so good, but in my actual program, I need some of the parameters to depend on some others:
params = (
a = 3.14
,b = "hello"
,c = func(a)
)
which the compiler complains with “a
not defined”.
You can fix this situation by
params = let
a = 3.14
(a = a, b = "hello", c = func(a))
end
This isn’t too bad, but if you have more parameters and more dependent ones, redundancy increases.
The good thing about the named tuple is that it’s simple and reads as a table of definitions with little redundancy. But, if you use the above let
construct, that virtue is diminished.
Is there an elegant solution?
So, currently I’ve started to abuse a nested module as a container:
module params
a = 3.14
b = "hello"
c = Main.func(a)
end
# . . . use params.a, etc. . . .
Would there be disadvantages in this solution?
I’ve used modules to share constants between different programs or different modules of a single program, but I’ve never thought of using it as if it were a NamedTuple.