Translating Matlab to Julia

I am translating some code from Matlab to Julia.

I found out that the author is using a lot of Matlab functions with the same name but different functionality, which are stored in different folders.
Before using these functions he adds the folders that shall be used to the search path. So he selects a set of functions that shall be used.

How can this be translated to Julia in a reasonable way?

make different “folders” different (sub)Modules in Julia?

2 Likes

He has 26 folders, and one function in each of these folders. In one of the folders are three functions. I don’t want to have 26 modules for 28 functions.

OK, if I create all theses modules, and executed using Directory_Constant, can I undo this using of a module because later I want to do using Directory_Constant_wErrorCov? Both folders/ modules would contain the same function with a different functionality.

It sounds like you might do well to use a trait:

# define your different modes for the function
struct Mode1 end
struct Mode2 end

# define the function for each mode
foo(::Mode1, x) = x+3
foo(::Mode2, x) = 2x

Then set mode = Mode1() and pass that variable through your functions.

If you really can’t be bothered to pass that variable around, you can use a global variable (will cause dynamic dispatch) or a constant function (will require recompilation via Revise.jl or restarting when changed but will dispatch statically).

globalmode() = Mode2() # constant function that sets the mode globally
foo(x) = foo(globalmode(), x) # run with the globalmode() configuration by default
5 Likes

This seams to work nicely! Thank you.