Redefine Function as Variable

Often I used my REPL for testing and scratch work. Thus, the variable x gets bound to quite a few things and I redefine it often. However, if I do:

function x(y)
return y+1
end

and then later try

x = 5

I am hit with:

ERROR: invalid redefinition of constant x

Looking online, I see that the workspace() function would have taken care of this nicely, but this is no longer available. Additionally, I can’t do:

x=nothing
GC.gc()

as I get the same error. (and apparently gc() doesn’t work anymore either). Any suggestions as to how I can circumvent this? I’d really not like to have to close out of the REPL. There are times I have packages precompiled and waiting a few minutes every time I run into this is somewhat annoying.

Thanks for your help.

Try defining the function inside a module. See https://docs.julialang.org/en/v1/manual/workflow-tips/.

1 Like

FWIW, the reason workspace used to be unreliable which ultimately lead to its removal (package being loaded into Main) has been changed so, and while I can’t speak for everyone, I would at least personally like to have it back.

Someone with the desire to do so could perhaps take a look at the PR that removed it and try to undo it.

4 Likes

Thanks, this is a great way to keep things separate and modular.