# Type stability within functions?

**URL:** <https://discourse.julialang.org/t/type-stability-within-functions/10970>\
**Category:** New to Julia\
**Tags:** type-stability, functions\
**Created:** [May 17, 2018, 10:48pm UTC](https://discourse.julialang.org/t/type-stability-within-functions/10970 "2018-05-17T22:48:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maalaria](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@maalaria](https://discourse.julialang.org/u/maalaria)\
**Post date:** [May 17, 2018, 10:48pm UTC](https://discourse.julialang.org/t/type-stability-within-functions/10970/1 "2018-05-17T22:48:47Z")

</div>

Hello, I am currently doing the first steps with Julia and stumbled over the following fact concerning type stability.  
When I do the following I get an invalid redefinition error as expected:

```julia
const dl = 3
dl = 3. 

```

but if I wrap it in a function

```julia
function assign_test(x_)
    const dl = 3
    dl = x_
end

assign_test(4.4)

```

I won’t get this error. Why is that?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 17, 2018, 10:54pm UTC](https://discourse.julialang.org/t/type-stability-within-functions/10970/2 "2018-05-17T22:54:19Z")

</div>

Yeah, `const` in a local scope does nothing currently (which is kind of silly). It’s deprecated in Julia `master` so that this particular confusion can be avoided: [https://github.com/JuliaLang/julia/pull/23259](https://github.com/JuliaLang/julia/pull/23259) and [https://github.com/JuliaLang/julia/issues/5148#issuecomment-320319147](https://github.com/JuliaLang/julia/issues/5148#issuecomment-320319147)

---

<div class="post-metadata">

**Author:** ![maalaria](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@maalaria](https://discourse.julialang.org/u/maalaria)\
**Post date:** [May 20, 2018, 12:52pm UTC](https://discourse.julialang.org/t/type-stability-within-functions/10970/3 "2018-05-20T12:52:52Z")

</div>

Thanks for your reply!

---

<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:** [May 20, 2018, 10:45pm UTC](https://discourse.julialang.org/t/type-stability-within-functions/10970/4 "2018-05-20T22:45:02Z")

</div>

If you do want to “set the type”, use a type-declaration:

```julia
function assign_test(x_)
    dl::Int = 3
    dl = x_
end

assign_test(4.4)

```

This now errors when trying to make `x_` an `Int`.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [May 20, 2018, 11:34pm UTC](https://discourse.julialang.org/t/type-stability-within-functions/10970/5 "2018-05-20T23:34:52Z")

</div>

> [@ChrisRackauckas](#):
>
> If you do want to “set the type”, use a type-declaration:
> 
> ```julia
> function assign_test(x_)
> dl::Int = 3
> dl = x_
> end
> 
> assign_test(4.4)
> 
> ```

Note, you can also use `local dl::Int` rather than a superfluous explicit assignment `dl::Int = 3`

Also note that while this “sets the type”, it will only error if the RHS cannot be `convert`ed on assignment.  
For example:

```julia
function assign_test(x_)
    local dl::Float64
    dl = x_
    return dl
end

assign_test(4)

```

will return `4.0` successfully
