I want to simulate two variants of a process that involves the same objects (e.g. a stochastic process on the unit interval [0,1) or on the circle \mathbb R/\mathbb Z).
For the first variant, I’ve grouped the objects defining the process in a struct System
and defined a function simulate(s::System)
that performs the simulation.
For the second variant, I only need to change few things in simulate
. Some possibilities on how to implement the second variant are the following ones:
- Modify
System
so that it contains an additional field, e.g. anEnum Variant
, which is then used bysimulate
to determine which variant to run; - Define another function
simulate2
; - Define a different struct
System2
and use multiple dispatch onsimulate
.
Q1. Any advice on what would be the most julionic way to do that?
I feel option 3 would be the most appropriate one. However, in that case I’d need two structs System2
and System1
which have the same fields, and differs only by the name of their type. Here some possibilities are the following ones:
- 3.1. Just repeat code.
- 3.2. Use code generation:
for type in (:System1, :System2)
eval(quote
struct $type
field1
...
end
end)
end
- 3.3. Create a parametric type
System{T}
and abstract typesVariant1
andVariant2
, and then declare the system withSystem{Variant1}(...)
etc.
Q2. Any advice on what would be the most julionic way to do that?
I feel that option 3.3 could be a misuse of the concept of parametric type, since the parametric type is not used in any field; on the other hand, if one wants to have the possibility to multiple dispatch on structs holding the same objects, it looks like a nice way to make the situation transparent.