Removing objects on memory, again. Alternatives to A = nothing?

Because we’re mean, nasty people :smiling_imp:

Nah, just kidding. It’s because being able to undefine things that have already been defined is pretty fundamentally incompatible with many assumptions that the Julia compiler makes when generating code. Those assumptions could potentially be relaxed but it would entail a lot of annoying work for a feature that isn’t strictly necessary and doesn’t really help you do anything.

It might be possible these days to implement this feature by creating a new Main module and copying all the bindings from the old Main into the new one except for the ones that you want to clear and then throwing out the old Main. However, there are a some major caveats to that approach:

  1. This won’t free any memory since there will still be references to the old Main module.
  2. It also won’t handle functions that refer to old globals correctly since the functions will still use the old versions of these globals in the old Main module.

So the better way to do this feature may be to just allow non-const globals to be put back into the undef state that globals can be in before they are first defined. That’s a significant compiler project, however, and pretty hard to motivate. Where would you rank it on the list of compiler work priorities? I find it hard to put it anywhere but the very bottom (even if we added quite a few other projects I can think of to the list). Of course, maybe it would be a good first project for someone looking to get into compiler work.

12 Likes