Outer constructor using a function using the structure

I have a problem with a “complex” outer constructor (for me anyway).

I have the following structure:

struct MQC
circuit_list::Vector{QuantumCircuit}
connector_list::Vector{Connector}
wire_list::Vector{Wire}
MQC() = new(Vector{QuantumCircuit}(), Vector{Connector}(), Vector{Wire}())
end

As you can see I have a very simple constructor creating empty vectors which the user can populate after with the two following two functions:

function MQCAddCircuit(mqc::MQC, newc::QuantumCircuit)::Bool
and
function MQCAddConnector(mqc::MQC, connec::Connector)::Bool

Both have, as an ultimate goal, to add elements in the two first Vector of the structure. But they have complicated consistency checking rules before performing the finale push!() and will return false and exist before if it fails to meet the requirements.

I would like to allow users to create an MQC structure by providing a vectors of circuits and a vector of connectors:
MQC(vector1::Vector{QuantumCircuit}(), vector2::Vector{Connector}()) = SOME_CODE.

(never mind the third vector, it’s for internal use and not seen by the user)

In SOME_CODE above, I want to call the two adding function in a loop to transfer the elements of vector1 and vector2 into the vectors of the structure.
How can I call MQCAddCircuit(mqc::MQC, newc::QuantumCircuit)?
In C++ language mqc would be “this” (if I may say so).

I need somehow to let the structure I am talking about itself. SOME_CODE would be like:
for circuit in vector1
MQCAddCircuit(this, circuit)
end
for connector in vector2
MQCAddConnector(this, connector)
end

Can a “C++ programmer converted to Julia” show me the light? Thanks.

Hello, can you please format your code using triple backticks? It’s hard to read (and even more on mobiles) like this…

2 Likes

It’s a bit difficult to follow, both because of the formatting but also because there’s a lot of extra information, and we can’t run it. If we dumb it down a bit:

struct MyType
    x::Vector{Int}
    MyType() = new(Int[])
end
# modify
function push!(m::MyType, i::Int)
    iseven(i) || return m
    push!(m.x, i)
    return m
end
# second constructor
function MyType(i::Int)
    m = MyType()
    push!(m, i)
    return m
end

Does this example look like what you want, or can you modify it such that it does?

I apologize for this. I didn’t know it could be done. I will do in the future.

Sorry for the late reply, I was away from work for a few days.

I guess that’s exactly what I am looking for. So simple after all.

Thanks.