# Two type composite struct construction

**URL:** <https://discourse.julialang.org/t/two-type-composite-struct-construction/43727>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 26, 2020, 10:50pm UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727 "2020-07-26T22:50:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 26, 2020, 10:50pm UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/1 "2020-07-26T22:50:00Z")

</div>

Hi.  
I have the following problem/question. I have a struct

```julia
struct foo{T<:Integer, U<:Real} <: AbstractGraph{T}
  bar1::T
  bar2::U
end

```

Now, I want to create an instance of foo, where I explicitly define the type `U` but infer the type `T` from the arguments. I tried

```julia
function foo{U}(...) where {U<:Real}
  # do some stuff here and get T
  foo{T,U}(bar1, bar2)
end

```

But this errored with asking for `T`. Now, what is the right way to construct `foo` with only passing `U` as a type?

EDIT1: See the marked answer and have a look at this thread as well: [https://discourse.julialang.org/t/how-to-correctly-define-and-call-templated-parametric-methods-using-the-new-where-syntax](https://discourse.julialang.org/t/how-to-correctly-define-and-call-templated-parametric-methods-using-the-new-where-syntax)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 26, 2020, 11:56pm UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/2 "2020-07-26T23:56:03Z")

</div>

Something like that?

```julia
function foo(::Type{U}, bar2) where {U<:Real}
    # do some stuff here and get T
    T = typeof(bar2)
    bar1 = zero(U)
    foo{T,U}(bar1, bar2)
end

```

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 27, 2020, 10:22am UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/3 "2020-07-27T10:22:08Z")

</div>

Hi @Henrique_Becker,

thanks, that was what I was looking for!

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 27, 2020, 4:45pm UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/4 "2020-07-27T16:45:24Z")

</div>

I noticed just now that this is basically [the first doubt I asked in Discourse](https://discourse.julialang.org/t/how-to-correctly-define-and-call-templated-parametric-methods-using-the-new-where-syntax/24129).

So the inquirer is now the replier. The cycle begins anew.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 27, 2020, 5:34pm UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/5 "2020-07-27T17:34:32Z")

</div>

Very good! Reading through your linked thread leaves me with another question. What is the difference between below definitions of `foo`

```julia
foo(::Type{U}) where {U<:Real}

```

and

```julia
foo(U::Type)

```

besides that `U` can by any type in the second example und only a subtype of real in the first?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [July 27, 2020, 9:04pm UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/6 "2020-07-27T21:04:55Z")

</div>

There is no difference. Actually, if you define foo at the REPL, you will see that after the second definition, there is still only one method!

```julia
julia> foo(::Type{U}) where U = 1
foo (generic function with 1 method)

julia> foo(U::Type) = 100
foo (generic function with 1 method)

```

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 27, 2020, 9:40pm UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/7 "2020-07-27T21:40:41Z")

</div>

That’s interesting. So, why are there two different syntax’?

---

<div class="post-metadata">

**Author:** ![lhnguyen-vn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lhnguyen-vn/32/15449_2.png) [@lhnguyen-vn](https://discourse.julialang.org/u/lhnguyen-vn)\
**Post date:** [July 27, 2020, 10:07pm UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/8 "2020-07-27T22:07:47Z")

</div>

As in your example, writing `Type{T} where T` gains you access to the type parameter either for dispatch purposes (`T <: Real`) or to use later inside your function.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [August 5, 2020, 12:36am UTC](https://discourse.julialang.org/t/two-type-composite-struct-construction/43727/9 "2020-08-05T00:36:31Z")

</div>

that makes sense! Great, thanks!
