How to delete a function?

I want to experiment with a small function. Before defining it, I want to make sure that the function name is not used before, or it does not exist in the workspace.

How can I do it? In Matlab, it is simple with ‘clear’

You can’t, unfortunately. There are some workarounds people use but there’s no obvious solution.

1 Like

If it errors, it does not exist

julia> blabla
ERROR: UndefVarError: `blabla` not defined
4 Likes

There’s no workspaces in Julia. If you mean module, including the default Main of the REPL, you can use hasproperty to check if it has a global variable and isdefined to check if it is also assigned, if you don’t want to check with an error. You can’t remove a variable from a module, even if you can reassign it to possibly free the previous instance for the garbage collector; this is important for optimizations and keeping code working.

In interactive practice, it’s fine to make a few throwaway const variables, and most of the time I’m just redefining methods of the same function. If you really find yourself in the position of making many distinct functions, you could avoid making as many variables by assigning anonymous functions to one non-const variable. You can’t add methods with a non-const name alone e.g. foo(x)=1, but you can add one with (::typeof(foo))(x) = 1. Just be aware that a non-const global variable will introduce runtime dispatch in callers, so I’d stick with top-level calls for experimentation with them.

2 Likes

As said by others:

  • If what you want is to make sure that the function name is not used before, or it does not exist, you can query with the function isdefined, or easier with the macro @isdefined (e.g. @isdefined foo to query if there is anything called foo in the current module`.
  • If you want to delete a function (or whatever other type of object’s) name, unfortunately you can’t. (There are other useful FAQs in the same page.)

The nearest thing to Matlab’s workspaces in Julia are modules. It is possible to switch between modules in the REPL, which is the closest that you’ll get to clear all in Matlab (without actually deleting anything), but it’s better to adapt your workflows so that you don’t feel the need of “deleting variables”, which is unnatural in Julia.

Since you are asking several questions related to transitioning from Matlab to Julia, let me recommend you to spend some time reading some of the many resources that the community has being producing for such situations. There is a thread from a few year ago where they started to comment on this:

And some other resources that might be useful too (you’ll find many more with any search engine):

https://en.wikibooks.org/wiki/Julia_for_MATLAB_Users

4 Likes

I think MATLAB’s workspaces are so particular, it’s better to just say they’re different from modules. This discrepancy isn’t by accident, MATLAB’s workspaces in part make some interactive practices like clearing much more feasible, though it’s worth reminding that MATLAB documentation discourages large clears. As I’ve written before, we can’t delete global variables without really bad consequences, for the same reason that MATLAB protects global variables into a separate global workspace away from the script’s base workspace.

There’s merit to seeking parallels between MATLAB and Julia functionality, but I think there is also merit to learning a language on its own terms enough to readily identify the reasons a feature is absent. This is just tougher when the languages have more fundamental differences, and MATLAB does many things differently from even other interactive high-level languages, with the exception of its clone Octave.

3 Likes

Simplest solution is to restart the REPL.

VSCode: Alt+J, Alt+R
Terminal: exit(), julia

see also Noteworthy Differences from other Languages · The Julia Language