Struct with Multiple Parametric Types

I have a few “operations” and I will read a CSV file to determine a list of operation to perform.

I also have a collection of operations “ManyOps” that hold multiple operations.

Operate → abstract type
Cut, Roll, Push → concrete type
ManyOps → concrete type with Parametric Types of Cut, Roll, Push

I want to avoid using abstract type “Vector{Operate}”, therefore I have to use Tuple of Operate.

But below generate an error, may I know how to fix it?

Ideally, I want to generate a ManyOps{Cut, Roll}, and the Cut and Roll can be variable size, read from a CSV file
e.g. the CSV file may contain: Cut, Roll, Cut, Roll, Push, Cut

Thank you

abstract type Operate end

struct Cut <: Operate
member::String
Cut() = Cut(“t”)
end

struct Push <: Operate
member::Int
Push() = Push(1)
end

struct Roll <: Operate
member::Symbol
Roll() = Roll(:a)
end

struct ManyOps{T}
ops::T
ManyOps(ops::T) where {T} = new{Union{map(typeof, ops)…}}(ops)
end
ManyOps(ops::Operate…) = ManyOps(ops)

ManyOps(Cut(), Roll())

You need to use new for the inner constructors of Cut and Roll.

How about wrapping a tuple?

abstract type Operate end

struct Cut <: Operate
    member::String
end
Cut() = Cut("t")

struct Push <: Operate
    member::Int
end
Push() = Push(1)

struct Roll <: Operate
    member::Symbol
end
Roll() = Roll(:a)

struct ManyOps{T<:Tuple{Vararg{<:Operate}}}
    ops::T
end

# ManyOps( ops...) = ManyOps( ops ) # Don't do this: It calls itself
ManyOps( op1::T ) where T<:Operate = ManyOps( (op1,) )
ManyOps( op1, op2, op3...) = ManyOps( (op1, op2, op3...) )
ManyOps( Cut(), Roll() )
ManyOps( 123, "x" ) # errors
1 Like