# Why type instability?

**URL:** https://discourse.julialang.org/t/why-type-instability/4013
**Category:** General Usage
**Tags:** question
**Created:** [May 31, 2017, 12:56pm UTC](https://discourse.julialang.org/t/why-type-instability/4013 "2017-05-31T12:56:57Z")
**Posts on this page:** 5
**Page:** 2

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 23, 2017, 11:08am UTC](https://discourse.julialang.org/t/why-type-instability/4013/21 "2017-11-23T11:08:59Z")

</div>

I would like a feature like this as well… Like an assert. @type\_stable\_assert 😛

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 23, 2017, 12:33pm UTC](https://discourse.julialang.org/t/why-type-instability/4013/22 "2017-11-23T12:33:27Z")

</div>

The hard part is finding a set of static rules for a subset of Julia, but it does seem like it might be possible to find such a rule set and let people declare sections of code to follow those rules and accordingly get guaranteed tight type bounds and compile-time type checking. Definitely an interesting area for future work.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 23, 2017, 12:50pm UTC](https://discourse.julialang.org/t/why-type-instability/4013/23 "2017-11-23T12:50:59Z")

</div>

On the other hand, you can type assert return types, `fun(x)::Int`, which automatically makes the function type stable. This is the way C guarantees type stability.

---

<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: [November 23, 2017, 1:26pm UTC](https://discourse.julialang.org/t/why-type-instability/4013/24 "2017-11-23T13:26:11Z")

</div>

> [@e3c6](#):
>
> you can type assert return types, fun(x)::Int

If you mean that this

```julia
function fun(x)::Int 
...
end

```

asserts the return-type, then you’re wrong (otherwise ignore me!). It converts the type:

```julia
julia> function f(x)::Int
       x
       end                                                                                                                                                              
f (generic function with 1 method)                                                                                                                                      

julia> f(Int32(3))                                                                                                                                                      
3                                                                                                                                                                       

julia> Int32(3)::Int                                                                                                                                                    
ERROR: TypeError: typeassert: expected Int64, got Int32                                                                                                                 

```

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 23, 2017, 1:36pm UTC](https://discourse.julialang.org/t/why-type-instability/4013/25 "2017-11-23T13:36:08Z")

</div>

My point is that it guarantees type-stability. Wherever you call `fun` you are sure the return type is an Int. Also, it only converts the type if it is “convertable”. `fun("hello")` throws an error.

[Previous page](https://discourse.julialang.org/t/why-type-instability/4013.md?page=1)
