# Type instability - two examples: yes/no?

**URL:** <https://discourse.julialang.org/t/type-instability-two-examples-yes-no/2680>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 15, 2017, 3:08pm UTC](https://discourse.julialang.org/t/type-instability-two-examples-yes-no/2680 "2017-03-15T15:08:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [March 15, 2017, 3:08pm UTC](https://discourse.julialang.org/t/type-instability-two-examples-yes-no/2680/1 "2017-03-15T15:08:56Z")

</div>

Which of these functions is type-unstable? If unstable:

1. What are the performance implications?
2. What’s the output in `@code_warntype` that would indicate instability?

(For the record: I know how to parameterize them; I’m trying to determine when parameterization makes sense.)

Thanks!

```julia
function foo1(v::Vector)
  T = eltype(v)
  return one(T)
end

```

```julia
function foo2(v::Vector, x::Integer)
  T = eltype(v)
  oneT = one(T) # explicit assignment here just to make sure we allocate mem.
  return (oneT < x)
end

```

---

<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:** [March 15, 2017, 3:31pm UTC](https://discourse.julialang.org/t/type-instability-two-examples-yes-no/2680/2 "2017-03-15T15:31:15Z")

</div>

They’re not unstable from what I can see. Parameterization doesn’t change functions. In fact, if you check the compiled code, `T` is definitely computed at compile time, so it’s no different than using a type parameter even in the compiled code.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 15, 2017, 3:32pm UTC](https://discourse.julialang.org/t/type-instability-two-examples-yes-no/2680/3 "2017-03-15T15:32:11Z")

</div>

> [@anon94023334](#):
>
> Which of these functions is type-unstable?

None. Assuming none of the functions you called are overwritten in a unstable way.

> [@anon94023334](#):
>
> 1. What are the performance implications?

For these, none. For type instability, it depends.

> [@anon94023334](#):
>
> 1. What’s the output in @code\_warntype that would indicate instability?

Red.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [March 15, 2017, 3:32pm UTC](https://discourse.julialang.org/t/type-instability-two-examples-yes-no/2680/4 "2017-03-15T15:32:57Z")

</div>

Thanks. I’m surprised that the first one isn’t type-unstable, since the type of the output depends on the result of a calculation within the function. Perhaps I have a faulty definition of type stability.

---

<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:** [March 15, 2017, 3:34pm UTC](https://discourse.julialang.org/t/type-instability-two-examples-yes-no/2680/5 "2017-03-15T15:34:24Z")

</div>

If `one(T)` is type-stable (it probably is for any type you’re using), then by knowing `T` (which is known because of dispatch, methods auto-specialize in Julia), you know the output type of `one(T)`. Thus you know the output type, and every intermediate type. It’s all strictly typed.
