# Combined declaration of multiple variables of the same type?

**URL:** <https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353>\
**Category:** General Usage\
**Created:** [September 22, 2018, 7:30pm UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353 "2018-09-22T19:30:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 22, 2018, 7:30pm UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353/1 "2018-09-22T19:30:53Z")

</div>

Is it possible to declare multiple variables of the same type in a C-style in Julia? I like to define a struct as this:

```julia
struct Tbp
  es::Float64
  ep::Float64
  vsss::Float64
  vsps::Float64
  vpps::Float64
  vppp::Float64
  n::Int
  nc::Float64
  rc::Float64
  r0::Float64
  r1::Float64
  phi0::Float64
  m::Float64
  mc::Float64
  dc::Float64
  d0::Float64
  d1::Float64
end

```

in one line like this

```julia
struct Tbp
   es, ep, vsss, ... :: Float64
end

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 22, 2018, 8:11pm UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353/2 "2018-09-22T20:11:52Z")

</div>

No. But you could define a macro to do this.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 22, 2018, 9:27pm UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353/3 "2018-09-22T21:27:15Z")

</div>

You can make this simultaneously easier to write and to read _and_ more generic as follows:

```julia
julia> struct MyType{T}
       a::T
       b::T
       c::T
       d::Int
       end

julia> MyType(3.1, 4.2, 5.3, 17)
MyType{Float64}(3.1, 4.2, 5.3, 17)

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 22, 2018, 9:33pm UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353/4 "2018-09-22T21:33:17Z")

</div>

Sure, this is more elegant than the original code, but still, one must repeat the `::T` all the time. To me, what I want seems a reasonable feature request for future versions of Julia, isn’t it?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 22, 2018, 9:47pm UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353/5 "2018-09-22T21:47:44Z")

</div>

No need to wait, as Mauro said you can do it already with a macro. (And check out his related package Parameters.jl.) And I doubt it would be included in the next version of Julia.

Often having so many variables in a type would be a sign that you need to redesign.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 22, 2018, 9:51pm UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353/6 "2018-09-22T21:51:40Z")

</div>

> [@mauro3](#):
>
> But you could define a macro to do this.

```julia
julia> macro TTime(T, vars...)
           expr = Expr(:block)
           append!(expr.args, map(var -> :($var::$T), vars))
           return esc(expr)
       end
@TTime (macro with 1 method)

julia> struct MyType{T}
           @TTime T x y z
       end

julia> MyType(1,2,3)
MyType{Int64}(1, 2, 3)

julia> fieldnames(MyType)
(:x, :y, :z)

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 22, 2018, 9:57pm UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353/7 "2018-09-22T21:57:29Z")

</div>

Many thanks, that is really nice.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 23, 2018, 1:07am UTC](https://discourse.julialang.org/t/combined-declaration-of-multiple-variables-of-the-same-type/15353/8 "2018-09-23T01:07:57Z")

</div>

That’s a beautifully simple solution!
