Stream: helpdesk (published)

Topic: Alias mutable const from another package


view this post on Zulip Timothy (Nov 03 2022 at 08:24):

Say I have a package A with the following:

module A
const foo = Dict(1 => 'a')
end

and I want to alias foo in a dependent package B

module B
using A
const foo = A.foo
end

if A.foo is then updated (e.g. via A.foo[2] = 'b') I find that B.foo holds the value of A.foo at (pre?)compile time, which is not what I want. Is there a way that B.foo can simply be an alias for A.foo ?

view this post on Zulip Sukera (Nov 03 2022 at 08:27):

do you want to reexport A.foo for users of B..?

view this post on Zulip Sukera (Nov 03 2022 at 08:29):

wait, how are you observing that change?

view this post on Zulip Sukera (Nov 03 2022 at 08:30):

if you have

module C
using A
using B
end

then the A.foo in C _should_ be different from the A.foo in B, I think

view this post on Zulip Sukera (Nov 03 2022 at 08:31):

regardless, this works:

julia> module A
       const foo = Dict(1 => 'a')
       end
Main.A

julia> module B
       using ..A: foo
       end
Main.B

julia> module C
       using ..A
       using ..B
       f() = @show A.foo === B.foo
       end
Main.C

julia> using .C

julia> C.f()
A.foo === B.foo = true
true

Last updated: Dec 28 2024 at 04:38 UTC