Older versions of custom functions are saved as methods

This may be simple, but as I’m now getting used to multiple dispatch, I want to be sure that I understand how to work properly.

I’m writing some custom functions for my project.

Let’s say I have the following function:

function myFun(arg1, arg2)
    return arg1+arg2

I run the code, and then realise that for my purposes, myFun should have three arguments instead of two. So I redefine myFun as:

function myFun(arg1, arg2, arg3)
    return arg1+arg2+arg3

When I run the code, I see that myFun now has two methods. Running methods(myFun), I see that the older version of myFun still exists as a method.

It’s not that I want to keep both, as two methods. The older version was incorrect for what I wanted to do and can possibly lead to some bugs, if it’s called instead of the updated version.

How do you deal with this? Do you delete the older methods? If yes, doing something like that? Is there an alternative, where the updated function deletes the older version?

Since the two methods take a different number of arguments, there is no way for julia to know that you intend to use one over the other. I’m guessing you’re just defining both in the REPL?

For functions in files, Revise.jl can take care of deletion for you, I think.

I’m defining them in a file, that’s why I’m having this question/issue.

So I’m just changing my file, and then running it again. I think it would be really helpful if julia could understand that I changed the function in the file, and the old one doesn’t exist.

I will check the package you mentioned, thanks!

If they are in a module, Revise can track them. If they’re in a file you’re include-ing into the REPL, you can use includet (include-and-track) from Revise and I believe that will still work.

I don’t remember the exact details, but I believe I’ve had the cleanest experience if the file I’m includet-ing has only function definitions and then I call those from the REPL (rather than containing functions and a test script). For example, create a main() function in the file that you then call from the REPL to run your test.

It may also work to repeatedly includet the file, although I recall that any script-like parts may not be re-run on subsequent indludets. You could try to include after includet to re-run but that may or may not cause issues with updating the methods.

How are you running the file again? Restarting julia should already clear the old state.

If you’re running it from e.g. VSCode, Revise.jl will definitely help you out - I think the julia extension also already has support for that.

I’m running it from VS Code.

Will definitely check Revise .

Thank you both very much for your answers!

I think Revise is loaded by default in VSCode. Then I think you just need to use includet(file.jl), instead of include (note the extra t - for track).

Then, you don’t need to includet repeatedly, just modify the file and run your function again.