Are Value types the correct solution for me? (as parameters that choose the algorithm)

I guess the simulation takes more than a few microseconds, thus probably both things there are not important. (the problem is using that inside a hot loop of your code, when the type changes).

A common pattern is to use:

julia> struct Method1 end

julia> struct Method2 end

julia> simulation(params,method::Method1) = println("method1!")
simulation (generic function with 1 method)

julia> simulation(params,method::Method2) = println("method2!")
simulation (generic function with 2 methods)

julia> simulation("abc",Method1())
method1!

julia> simulation("abc",Method2())
method2!

or, alternatively, you could parameterize the Params with the method, such as:

julia> struct Params{Method} 
           method::Method
       end

julia> simulation(params::Params{Method1}) = println("method 1!")
simulation (generic function with 3 methods)

julia> simulation(params::Params{Method2}) = println("method 2!")
simulation (generic function with 4 methods)

julia> simulation(Params(Method1()))
method 1!

julia> simulation(Params(Method2()))
method 2!

I think the choice must be on the basis on how clean the input becomes for the user, in your case.

(from the perspective of code sharing between the methods there is no difference between any of the alternatives)

1 Like