# Help newbie to understand type stability please

**URL:** https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119
**Category:** General Usage
**Created:** [November 17, 2017, 10:55am UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119 "2017-11-17T10:55:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [November 17, 2017, 10:55am UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119/1 "2017-11-17T10:55:23Z")

</div>

Could anybody please help me improve my understanding of what looks like strange behavior?

```julia
function good1(x::Array{T,1}) where T<:Float64
    n = length(x)
    x *= 1 # problem is not here
    [i for i in x]
end

function good2(x::Array{T,1}) where T<:Float64
    n = length(x)
    [x[i] for i in 1:n] # problem is not here
end

function wrong(x::Array{T,1}) where T<:Float64
    n = length(x)
    x *= 1 
    [x[i] for i in 1:n]
end

@code_warntype good1([0.])
@code_warntype good2([0.])
@code_warntype wrong([0.]) # also n is interpreted as Any here!

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 17, 2017, 11:14am UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119/2 "2017-11-17T11:14:36Z")

</div>

You make `x` change its type. Did you mean `x .*= 1`? Or you can use another variable, as in

```julia
function good3(x::Array{T,1}) where T<:Float64
    n = length(x)
    y = x * 1
    [y[i] for i in 1:n]
end

```

Also see [this suggestion](https://docs.julialang.org/en/latest/manual/performance-tips/#Avoid-changing-the-type-of-a-variable-1) in the manual.

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [November 17, 2017, 11:26am UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119/3 "2017-11-17T11:26:11Z")

</div>

But why it is not a problem in good1?

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [November 17, 2017, 11:40am UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119/4 "2017-11-17T11:40:21Z")

</div>

BTW. I asked because I liked to give good answer on [SO](https://stackoverflow.com/questions/47346414/type-stability-for-a-function-involving-case-distinctions/47348191#47348191).

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [November 17, 2017, 11:56am UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119/5 "2017-11-17T11:56:43Z")

</div>

Another subquestion: Why is y “coreboxed” in next code? What does it mean? Or where could I find good documentation about it?

```julia
function tst(x::Array{T,1}) where T<:Float64
    n = length(x)
    n == 1 && return [1.0]
    y = x * 1
    [y[i] for i = 1:n]
end
@code_warntype tst([1.])

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [November 17, 2017, 12:19pm UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119/6 "2017-11-17T12:19:53Z")

</div>

Looks like something goes wrong. Somewhat simplified:

```julia
function f(x)
    return [x[i] for i = 1:length(x)]
end

function g(x)
    x = x
    return [x[i] for i = 1:length(x)]
end

@code_warntype f([0])
@code_warntype g([0])

```

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [November 17, 2017, 12:21pm UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119/7 "2017-11-17T12:21:08Z")

</div>

[https://github.com/JuliaLang/julia/issues/15276](https://github.com/JuliaLang/julia/issues/15276)

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [November 17, 2017, 12:48pm UTC](https://discourse.julialang.org/t/help-newbie-to-understand-type-stability-please/7119/8 "2017-11-17T12:48:24Z")

</div>

That (open!) issue has interesting milestone (0.5). (Probably label 1.0 is missing too?)

And yes - `let` could help avoid `Core.Box`. Thanks! 🙂
