# Declare multiple variables type like C++

**URL:** <https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 5, 2017, 11:55pm UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551 "2017-12-05T23:55:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [December 5, 2017, 11:55pm UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/1 "2017-12-05T23:55:33Z")

</div>

In C++, I can declare my variables in one line like:

`int x, y, z;`

In Julia, it seems that I have to declare their types one by one,

```julia
x :: Int64
y :: Int64
z :: Int64

```

Can I do this at once?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 6, 2017, 12:02am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/2 "2017-12-06T00:02:06Z")

</div>

Is it important to have it only in one line or repeat the type only once? For the former:

```julia
x::Int64; y::Int64; z::Int64

```

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [December 6, 2017, 12:03am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/3 "2017-12-06T00:03:31Z")

</div>

The latter one

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [December 6, 2017, 12:30am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/4 "2017-12-06T00:30:38Z")

</div>

Consider in C all of the confusion caused by: `char *src = 0, c = 0;` and `int i, j = 1;`

I have a funny feeling the C++ committee would have removed that feature if they could.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 6, 2017, 1:01am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/5 "2017-12-06T01:01:27Z")

</div>

You could write a macro `@deftype Int x y z`

---

<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:** [December 6, 2017, 1:54am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/6 "2017-12-06T01:54:08Z")

</div>

This is not something that you need to do in Julia. What is the use case?

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [December 6, 2017, 3:35am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/7 "2017-12-06T03:35:18Z")

</div>

I am used to the C++ and Fortran coding style, and just want to know if I can do this in Julia.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 6, 2017, 3:42am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/8 "2017-12-06T03:42:38Z")

</div>

Hi @Yifan_Liu,

Keep in mind @jlperla’s comment on the unclear expressions that you can write in C++. You may be saving yourself a few key strokes today, but whoever needs to read your code in the future will waste time, including yourself.

Also, as @dpsanders mentioned, Julia takes care of the types for you, you don’t need to be explicit about it. Let the language figure it out. 🙂

---

<div class="post-metadata">

**Author:** ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)\
**Post date:** [December 6, 2017, 3:48am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/9 "2017-12-06T03:48:45Z")

</div>

From time to time, I feel like that in Julia if I code like C++/ Fortran style my code will run faster, if I code like python style my code will run slower.

Is it my illusion or something that makes a little bit sense… I do not know 😴

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 6, 2017, 4:19am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/10 "2017-12-06T04:19:18Z")

</div>

By adding the types you are helping the compiler to not make mistakes, I guess. But in general this gap in performance that you see should go away with future releases of the language? I am not the most qualified person to answer your question, but I do agree that sometimes you need to go a little more explicit to get an extra power.

What happens sometimes is that you end up overwriting variables like in:

```julia
a = 1
a = a + 1. # oops, changed from Int to Float64

```

and this compromises performance. When you are explicit about the types, I think you can’t make these mistakes easily. The good news is that you are still writing the same language, you don’t need to resort to another compiler to off-load the computation. Start without type annotation and add explicit types if you really need it. 😉

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 6, 2017, 5:44am UTC](https://discourse.julialang.org/t/declare-multiple-variables-type-like-c/7551/11 "2017-12-06T05:44:17Z")

</div>

> [@juliohm](#):
>
> By adding the types you are helping the compiler to not make mistakes, I guess. But in general this gap in performance that you see should go away with future releases of the language? I am not the most qualified person to answer your question, but I do agree that sometimes you need to go a little more explicit to get an extra power.

It’s very very rare that you need to be explicit with types, and in any case that happens I think there’s a bug report open for it. But in most cases that users of Julia will rub into (except maybe the closure bug), adding type information will not help the compiler make things faster if the variables are not global.

Adding type declarations for safety is a different thing, but you could also just use `@code_warntype` and fix any instabilities at the root of the issue.
