I believe this has been asked and answered before; however I’ve gone through many of the related posts and I still can’t find how to do it. In my case, I had an array called T, which after a while I didn’t need. What I wanted to do was to create a function called T instead. I was told that
julia> T = nothing
would clear it, which it sort of does - at least it clears all memory allocated to it. But if I then try
julia> T(n) = n+1
say, I receive the error:
ERROR: cannot define function T; it already has a value
Is there any way of completely removing a user variable from the namespace? Or of writing over it?
I know I could stop and restart Julia, but I have a number of variables and functions I’ve been playing around with (I do a lot of experimenting in the REPL), that I don’t want to have to rewrite in a new session. So for me, clearing or removing a variable would be very useful indeed. Thanks!
Thank you - well, I guess that means I might have to re-consider how I use Julia. In my current session varinfo() lists about 38 user-defined variables and functions. This means that I can’t re-use, as it were, any previously used names for something else. No doubt this is a design choice made for excellent reasons.
Variable bind to things, which are all instances of types. E.g. 1 is such an instance, or as well the function sin. Variables (or more accurately “bindings”) which are const cannot be redefined. Also a variable which was non-const before cannot become const later. And as I said before, function definitions implicitly make const bindings, thus the problem you ran into.
However, just non-const variable can be re-assigned no problem:
julia> a = 5
5
julia> a = "Asdf"
"Asdf"
julia> const a = [1] # errors because of above explained stuff
ERROR: cannot declare a constant; it already has a value
Stacktrace:
[1] top-level scope at REPL[3]:1
julia> a() = 1 # errors
ERROR: cannot define function a; it already has a value
Stacktrace:
[1] top-level scope at none:0
[2] top-level scope at REPL[4]:1
All in all, it’s sometimes a little bit annoying but not really all that much of a problem.
Note this is also almost never a problem if you follow naming conventions (like those suggested in the style guide) Aside from readability, it helps make sure your bindings don’t bump into each other
Yes, that’s very good advice indeed - thank you! The trouble is that I’m often chopping and changing between different language REPL’s (currently I am running Julia, Python, GNU Maxima, GNU Octave, all in different tmux panes) and I tend to use the same slapdash style in all of them. And of those four, I believe Julia is the only one without a variable clearing mechanism. However, I could certainly do with a slightly more disciplined approach, and maybe if I am more disciplined in my use of Julia I could be equally more disciplined in my use of other systems…