How to check whether a variable is reassignable or not? (e.g. whether it is a function definition)

I have a module A and want to copy things from it to module B such that everything can be changed like before (this is needed inside Pluto).

For this goal, I need to know which variable is actually a variable, because it can be reassigned with plain myvariable = "newvalue", or whether it is a function definition, where reassignments fail, but function redefinition like myvariable() = "newvalue" work.

Any help how to find out that a variable (given by module and symbol) is reassignable is highly appreciated.

Those are all variables, maybe you’re referring to const variables where reassignment is either prohibited or warns of errors. Maybe isconst(::Module, ::Symbol) works for you.

2 Likes

yes, this seems to be it! thank you so much.

for clarity

julia> f() = 4
f (generic function with 1 method)

julia> f = 5
ERROR: invalid redefinition of constant f
Stacktrace:
[1] top-level scope
@ REPL[2]:1

julia> isconst(Main, :f)
true

is there also a way to distinguish whether assignment would produce a Warning vs an Error?

Not that I know of, without actually trying the assignment and catching the error. Warnings are usually Julia being nice and accepting the reassignment, as in

julia> const foo = 5
5

julia> const foo = 4
WARNING: redefinition of constant foo. This may fail, cause incorrect answers, or produce other errors.
4

The current implementation only errors for a type mismatch, so you can test on that, but this is an internal detail that may change at any point.

1 Like

See also recent How to check if variable is a typed global?

1 Like