Hola Ferran
Maybe using a constructor with optional arguments solves your use case?
julia> mutable struct Coly1
x :: Int32
y :: Float64
z :: Bool
t :: Int32
u :: Float32
v :: Bool
end;
julia> function Coly1(;x=0,y=0,z=false,t=0,u=0,v=false)
return Coly1(x,y,z,t,u,v)
end
# We can create a Coly1 instance specifying only one of the arguments.
julia> p = Coly1(y=3.14)
Coly1(0, 3.14, false, 0, 0.0f0, false)
# Or a subset of the arguments
julia> p2 = Coly1(y=3.14,z=true)
Coly1(0, 3.14, true, 0, 0.0f0, false)
# Or none of the arguments
julia> p2 = Coly1()
Coly1(0, 0.0, false, 0, 0.0f0, false)
it is true that the consturctor Coly1(;x=0,y=0,z=false,t=0,u=0,v=false)
has some predefined values but this happens as well in other languages like Python:
>>> class ColyPy(object):
... def __init__(self, x, y=0):
... self.x = x
... self.y = y
# This does not break because y has a predefined value
>>> p = ColyPy(x=2)
# This breaks because x has no value
>>> ColyPy(y=2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'x'
If you want to force a user to specify some of the arguments in a struct (like the pyhon case above)
mutable struct Coly4
x :: Int32
y :: Float64
z :: Bool
t :: Int32
u :: Float32
v :: Bool
end;
function Coly4(x;y=0,z=false,t=0,u=0,v=false)
return Coly4(x,y,z,t,u,v)
end
# This will not work because x is necessary
julia> Coly4()
MethodError: no method matching Coly4()
# You can still specify only x, (the first argument), to construct a Coly4
julia> Coly4(2)
Coly4(2, 0.0, false, 0, 0.0f0, false)