Translating Matlab to Julia

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