# Multiple type declarations with if-else

**URL:** https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759
**Category:** General Usage
**Tags:** type
**Created:** [March 9, 2020, 5:15pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759 "2020-03-09T17:15:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ykc](https://avatars.discourse-cdn.com/v4/letter/y/8edcca/32.png) [@ykc](https://discourse.julialang.org/u/ykc)
#### Post date: [March 9, 2020, 5:15pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/1 "2020-03-09T17:15:24Z")

</div>

I understand the proper practice is to pre-allocate outputs, but why wouldn’t the following work?

```julia
function f()
    if 1 == 1
        x::Float64 = 1.0
    else
        x::Float64 = 2.0
    end
    return x
end

```

This returns the following error message:

```julia
syntax: multiple type declarations for "x"

```

yet I don’t think I am really declaring types multiple times, since `x` is only created once.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 9, 2020, 5:25pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/2 "2020-03-09T17:25:36Z")

</div>

I don’t know why you’re not allowed to do that, but if you want, you can do `x = (1 == 1 ? Float64(1) : Int64(2))`, since the function doesn’t have type stability anyway

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 9, 2020, 5:25pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/3 "2020-03-09T17:25:46Z")

</div>

There’s a distinction between the assignment and the declaration of the name `x`. Yes, you’ll only ever assign to `x` once, but that one `x` name is the same throughout the entire function (including _before_ the assignment, amusingly enough). While the program flow will only ever see one of the two branches, the syntax applies to the entire function.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [March 9, 2020, 5:58pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/4 "2020-03-09T17:58:50Z")

</div>

I think the format `x::Float64` is basically saying you want to declare a variable of type x as Float64. If you change the code to:

```julia
function f()
    if 1 == 1
        x = Float64(1.0)
    else
        x = Float64(2.0)
    end
    return x
end

```

That works, and it’s doing what you expect the original function to do. i.e. X is of type Float64 but I don’t specifically declare it.

Yes you (because you are human) can inspect your original code and know what you expect to happen. The compiler apparently just sees you declaring X twice and doesn’t want to play that game.  
At a guess, the compiler programmers probably felt that if you are declaring the same variable twice that it was likely in error and rather than make a best guess at what you wanted to do, decided an error would be better so you could look at the code and determine what you really wanted to do.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 9, 2020, 6:08pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/5 "2020-03-09T18:08:01Z")

</div>

> [@ykc](#):
>
> I understand the proper practice is to pre-allocate outputs

It’s also worth noting that this is not pre-allocating an output. That advice applies to arrays; here you’re working with a single immutable number that won’t need allocation at all.

You almost never need to declare types on variables — it almost never has a speed impact.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [March 9, 2020, 6:17pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/6 "2020-03-09T18:17:05Z")

</div>

> [@mbauman](#):
>
> You almost never need to declare types on variables — it almost never has a speed impact.

I would say here that you are correct in that you don’t declare types on a variable for performance. You may however declare types on a variable to avoid coding pitfalls, i.e. you can’t accidentally change the data type if it was declared.

---

<div class="post-metadata">

### Author: ![ykc](https://avatars.discourse-cdn.com/v4/letter/y/8edcca/32.png) [@ykc](https://discourse.julialang.org/u/ykc)
#### Post date: [March 9, 2020, 6:24pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/7 "2020-03-09T18:24:22Z")

</div>

Thank you all for the swift replies! Yes I was declaring types mostly to avoid running into mistakes when using the package `ForwardDiff`, and came across this problem when I was creating arrays with type `Real` (bad for speed, but that’s what the package requires).  
So is it correct to infer from @mbauman’s answer that types are determined when names are first put to use, rather then when variables are created by associating the names to values?

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 9, 2020, 6:37pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/8 "2020-03-09T18:37:52Z")

</div>

Types are attached to objects. The number `2.0`, for example, has the type `Float64`. Julia’s compiler will go through your function and realize, for example that the name `x` is only ever used to refer to things of type `Float64` and thus it can apply a whole host of optimizations (like not needing to look up what `+` method to use, for example).

You don’t need to use `Array{Real}` to work with `ForwardDiff` and can work directly with arrays of Duals. For example, you really shouldn’t specify `::Array{Real}` in your function argument lists, but it’s hard to say what you may be doing here.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [March 9, 2020, 8:51pm UTC](https://discourse.julialang.org/t/multiple-type-declarations-with-if-else/35759/9 "2020-03-09T20:51:12Z")

</div>

If you like this very verbose style where you declare local variables and types, you can always do this:

```julia
function f(a)
    local x::Float64
    if 1 == 1
        x = 1.0
    else
        x = 2.0
    end
    return x
end

```

Now `x` will always be a `Float64` in the scope of `f`:

```julia
function g()
    local x::Float64
    if 1 == 1
        x = 10
    else
        x = 2.0
    end
    return x
end

julia> g()
10.0

```
