# How to best define a type with an argument not filled by the user

**URL:** <https://discourse.julialang.org/t/how-to-best-define-a-type-with-an-argument-not-filled-by-the-user/74442>\
**Category:** New to Julia\
**Created:** [January 11, 2022, 10:15pm UTC](https://discourse.julialang.org/t/how-to-best-define-a-type-with-an-argument-not-filled-by-the-user/74442 "2022-01-11T22:15:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benoitseron](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@benoitseron](https://discourse.julialang.org/u/benoitseron)\
**Post date:** [January 11, 2022, 10:15pm UTC](https://discourse.julialang.org/t/how-to-best-define-a-type-with-an-argument-not-filled-by-the-user/74442/1 "2022-01-11T22:15:12Z")

</div>

I’m looking to defined types which have certain fields, such as a matrix, and that constructs that matrix according to certain rules at the moment of type definition. Thus the user need not give the matrix, as it will be filled anyways. The way I found to do it is the following, but it feels wrong; is there a cleaner way?

```julia
struct RandHaar <: Interferometer
    m::Int
    U::Matrix{ComplexF64}
    RandHaar(m,U) = new(m,rand_haar(m)) #the “rand_haar” function gives a specific matrix (sampled according from the haar measure in this case)
end

RandHaar(n) = RandHaar(n, Matrix{ComplexF64}(undef,n,n))

```

---

<div class="post-metadata">

**Author:** ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)\
**Post date:** [January 11, 2022, 10:17pm UTC](https://discourse.julialang.org/t/how-to-best-define-a-type-with-an-argument-not-filled-by-the-user/74442/2 "2022-01-11T22:17:54Z")

</div>

If you don’t even want the user to have the option of providing the matrix than you can just replace the inner constructor with your function

```julia
struct RandHaar <: Interferometer
    m::Int
    U::Matrix{ComplexF64}
    RandHaar(n) = new(n, Matrix{ComplexF64}(undef,n,n))
end

```

With this `RandHaar(5)` will work as you expect and `RandHaar(5,someMatrix)` will error

---

<div class="post-metadata">

**Author:** ![benoitseron](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@benoitseron](https://discourse.julialang.org/u/benoitseron)\
**Post date:** [January 11, 2022, 10:20pm UTC](https://discourse.julialang.org/t/how-to-best-define-a-type-with-an-argument-not-filled-by-the-user/74442/3 "2022-01-11T22:20:30Z")

</div>

Sorry I wasn’t clear in my example : the “rand\_haar” function gives a specific matrix (sampled according from the haar measure in this case), and I don’t see how to apply it with your solution?

---

<div class="post-metadata">

**Author:** ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)\
**Post date:** [January 11, 2022, 10:29pm UTC](https://discourse.julialang.org/t/how-to-best-define-a-type-with-an-argument-not-filled-by-the-user/74442/4 "2022-01-11T22:29:21Z")

</div>

Ah, I misunderstood your original code. It should then be as simple as making the inner constructor only a single argument and eliminating the outer constructor. (If I’m interpreting your desire correctly).

```julia
struct RandHaar <: Interferometer
    m::Int
    U::Matrix{ComplexF64}
    RandHaar(m) = new(m,rand_haar(m)) 
end

```

---

<div class="post-metadata">

**Author:** ![benoitseron](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@benoitseron](https://discourse.julialang.org/u/benoitseron)\
**Post date:** [January 11, 2022, 10:49pm UTC](https://discourse.julialang.org/t/how-to-best-define-a-type-with-an-argument-not-filled-by-the-user/74442/5 "2022-01-11T22:49:50Z")

</div>

Yes, this works, thanks a lot!
