# Outer constructor using a function using the structure

**URL:** <https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260>\
**Category:** New to Julia\
**Tags:** constructors\
**Created:** [October 4, 2022, 11:20pm UTC](https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260 "2022-10-04T23:20:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgagnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgagnon/32/45466_2.png) [@dgagnon](https://discourse.julialang.org/u/dgagnon)\
**Post date:** [October 4, 2022, 11:20pm UTC](https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260/1 "2022-10-04T23:20:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 5, 2022, 4:52am UTC](https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260/2 "2022-10-05T04:52:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 5, 2022, 5:46am UTC](https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260/3 "2022-10-05T05:46:34Z")

</div>

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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![dgagnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgagnon/32/45466_2.png) [@dgagnon](https://discourse.julialang.org/u/dgagnon)\
**Post date:** [October 10, 2022, 10:24am UTC](https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260/4 "2022-10-10T10:24:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dgagnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgagnon/32/45466_2.png) [@dgagnon](https://discourse.julialang.org/u/dgagnon)\
**Post date:** [October 10, 2022, 10:35am UTC](https://discourse.julialang.org/t/outer-constructor-using-a-function-using-the-structure/88260/5 "2022-10-10T10:35:59Z")

</div>

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.
