# Purposes of explicit type annotations

**URL:** <https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105>\
**Category:** New to Julia\
**Created:** [May 25, 2020, 4:07am UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105 "2020-05-25T04:07:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [May 25, 2020, 4:07am UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105/1 "2020-05-25T04:07:55Z")

</div>

From the manual:

> Adding [type] annotations serves three primary purposes: to take advantage of Julia’s powerful multiple-dispatch mechanism, to improve human readability, and to catch programmer errors.

I’m curious why memory and computational efficiency is not listed there. For example, having image data represented by a default `Int64` pixels would be a huge memory waste and incur significant performance penalty in case where `UInt8` would be sufficient.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 25, 2020, 4:15am UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105/2 "2020-05-25T04:15:14Z")

</div>

IIRC, this is talking about type annotations to function arguments where this isn’t a concern, not types. That said, i may be wrong. Where in the docs is this?

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [May 25, 2020, 4:19am UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105/3 "2020-05-25T04:19:37Z")

</div>

Chapter 18, Types, at the beginning (3rd paragraph). My understanding is that it applies to all uses of explicit typing.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [May 25, 2020, 4:28am UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105/4 "2020-05-25T04:28:31Z")

</div>

On the third reading I can see that efficiency was mentioned a bit earlier:

> Julia’s type system is dynamic, but gains some of the advantages of static type systems by making it possible to indicate that certain values are of specific types. This can be of great assistance in generating efficient code

Maybe the sentence I quoted should be changed to:

> Adding [type] annotations to functions serves three primary purposes: to take advantage of Julia’s powerful multiple-dispatch mechanism, to improve human readability, and to catch programmer errors.

---

<div class="post-metadata">

**Author:** ![virtualgraham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/virtualgraham/32/15200_2.png) [@virtualgraham](https://discourse.julialang.org/u/virtualgraham)\
**Post date:** [May 25, 2020, 4:51am UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105/5 "2020-05-25T04:51:47Z")

</div>

> [@pauljurczak](#):
>
> I’m curious why memory and computational efficiency is not listed there.

While type annotations can achieve higher efficiency, its not guaranteed to. Its also possible to decrease efficiency with annotations. Perhaps by using types that overly large.

---

<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 25, 2020, 7:03am UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105/6 "2020-05-25T07:03:24Z")

</div>

> [@pauljurczak](#):
>
> For example, having image data represented by a default `Int64` pixels would be a huge memory waste and incur significant performance penalty in case where `UInt8` would be sufficient.

Sure, but that’s not what’s being discussed in that section. Including a few more lines of context:

> Thus, one can write many useful Julia functions without ever explicitly using types. When additional expressiveness is needed, however, it is easy to gradually introduce explicit type annotations into previously “untyped” code. Adding annotations serves three primary purposes: to take advantage of Julia’s powerful multiple-dispatch mechanism, to improve human readability, and to catch programmer errors.

The point being made here is that the _functions_ `f(x::Vector{Int})` and `g(x::Vector{<:Integer})` and `h(x::AbstractVector)` and `k(x)` will all give _the same performance_ (for the same input). The fact that a function has a type annotation on its input is useful as a way to control multiple dispatch or signal to a user what kind of data they should provide, but it’s not necessary for performance in Julia.

Another way of putting this is that the performance of a function does depend on the values you provide to it (as in the Int vs UInt8 example you mention), but it generally does not depend on whether or not you provided a type annotation to its arguments when you defined that function.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [May 25, 2020, 3:01pm UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105/7 "2020-05-25T15:01:55Z")

</div>

> [@rdeits](#):
>
> Sure, but that’s not what’s being discussed in that section.

It wasn’t clear to me on the first two readings. Perhaps making it explicit, would help the next reader: _Adding type annotations to function parameters serves three primary purposes…_

> [@rdeits](#):
>
> Another way of putting this is that the performance of a function does depend on the values you provide to it (as in the Int vs UInt8 example you mention), but it generally does not depend on whether or not you provided a type annotation to its arguments when you defined that function.

What is a case where performance of a function does depend on type annotation of its parameters?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 25, 2020, 3:13pm UTC](https://discourse.julialang.org/t/purposes-of-explicit-type-annotations/40105/8 "2020-05-25T15:13:50Z")

</div>

> [@pauljurczak](#):
>
> What is a case where performance of a function does depend on type annotation of its parameters?

[https://docs.julialang.org/en/v1.6-dev/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing-1](https://docs.julialang.org/en/v1.6-dev/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing-1)
