4
votes

How do you remove a function you have created when working in the Julia REPL? Can this be done without restarting the session? This came up for me because I created a function with the wrong type input, then made it correctly, but couldn't get rid of the old one.

The FAQ at http://docs.julialang.org/en/release-0.4/manual/faq/ states:

Julia does not have an analog of MATLAB’s clear function; once a name is
defined in a Julia session (technically, in module Main), it is always present.
3
When developing, I consider the REPL a sandbox for development. When the code becomes stable and safely tucked in .jl files, it can be re-run in a fresh Julia interpreter. Take home: be ready to restart occasionally. This would clear up any residual namespace pollution and is also important for replicability.Dan Getz
@user3580870 I guess this gets to a bigger question about how to use the REPL. I am accustomed to analyzing datasets in R, and like to load them up and analyze them with both library and my own designed functions. In this situation, it would be nice to be able to be able to get rid of (my) poorly designed functions without reloading a huge dataset.ultradian

3 Answers

3
votes

You cannot remove a function without restarting the REPL (Julia v0.4). The same goes for data types.

This has to do with the way Julia's type mechanism works, but unfortunately I know too little about it for a more detailed explanation.

1
votes

Assuming you have defined a function

xx(x) = 42

you can find the method with

m = @which xx(1)

and remove it with

Base.delete_method(m)

Note Base.delete_method(xx) won't work, i.e. you won't get rid of the new function (with name xx) like this:

julia> xx
xx (generic function with 0 methods)

=> The function name xx is still known, but the method you defined is gone. But for deleting a "function with the wrong type input" as you asked for, this would probably be the solution you want.

(Based on this thread.)

0
votes

Here is the reference the Julia documentation is referring to.

There is no method that would remove a function so the best option is to restart the terminal.

https://docs.julialang.org/en/latest/manual/faq/#how-do-i-delete-an-object-in-memory

Hope it changes soon