Overwriting functions in another module

Redefining some module’s values/functions directly is not allowed:

module testm

s = 1

function f(x)


testm.s = 3

ERROR: cannot assign variables in other modules

g = x -> x/10

testm.f = g

ERROR: cannot assign variables in other modules

However, this works:
testm.f(x) = g(x)



What is the reasoning behind such behavior? Thanks!

I filed an issue about it https://github.com/JuliaLang/julia/issues/23295. I find globals occasionally convenient.

There’s two work-arounds, either you use

const my_flag = Ref(false)

like Revise.jl does or you use @eval ThatModule some_global = 10.

Regarding testm.f = g, there’s a technical reason for not allowing assigning to a function. Julia has multiple dispatch, and it would make the compiler’s job much harder/impossible if it gave you the option to redefine functions like that.

You can also use setter functions:

julia> module A
       glob = 1
       change_glob(v) = global glob = v

julia> A.glob

julia> A.change_glob(4)

julia> A.glob

1 Like

Thanks! I’m still trying to understand the reason for how testm.f(x) = g(x) actually work as “redefine test.f” though.

You’re not allowed to change variables in other modules, but you are allowed to add/modify methods for functions defined in other modules.

see Methods · The Julia Language


Got it. Thanks!

You can’t write testm.f(x) = g(x) inside of a function, to redefine it at runtime. This restriction allows the compiler to assume that, once you’re pressed Enter at the REPL, f(x) = g(x) will be true until the computation finishes (barring some technicality with @eval and the world age).

Globals can be changed at runtime anywhere, so the compiler can’t assume anything about them, and their performance is terrible.

That’s why functions and globals are separate concepts in Julia, whereas in Python function definitions are (basically) implemented as global variables.


I am not sure what the original design reason was, but I like it this way. I consider globals as internal to a module, mostly an implementation detail, which should only be exposed via accessor functions outside the module.

Eg suppose I have a package for managing some templates, and have a setup like

module TemplateManager

export default_template_dir

DEFAULT_TEMPLATE_DIR = expanduser("~/templates")

default_template_dir() = DEFAULT_TEMPLATE_DIR


Later on I can decide to make the global a Ref, fixing the type, or allow the user to set it via an environment variable and just provide a fallback, eg

function default_template_dir()

without changing the API. Similarly, I can make a setter function

function default_template_dir!(path)
    isdir(path) || @warn "setting non-existent path for templates"

that validates the input.

1 Like

I’ve been experimenting a lot with similar issues related to dependency injection, and my current preferred approach is to eval within the scope of the targeted module. So if you’re looking for a way to actually do what you tried, this works:

julia> Core.eval(testm, :(s = 3))

julia> testm.s

Or TestModule.eval(:(s = 3)) (note that A.@eval s=3 does not work).

Nice, shorter.

Or if you want to write more complex expressions:

@eval TestModule begin
    s = 3
1 Like