# Are immutable types always passed by copying?

**URL:** <https://discourse.julialang.org/t/are-immutable-types-always-passed-by-copying/6013>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 20, 2017, 11:37pm UTC](https://discourse.julialang.org/t/are-immutable-types-always-passed-by-copying/6013 "2017-09-20T23:37:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [September 20, 2017, 11:37pm UTC](https://discourse.julialang.org/t/are-immutable-types-always-passed-by-copying/6013/1 "2017-09-20T23:37:01Z")

</div>

The [documentation](https://docs.julialang.org/en/stable/manual/types/#Mutable-Composite-Types-1) says

> An object with an immutable type is passed around (both in assignment statements and in function calls) by copying, whereas a mutable type is passed around by reference.

Is this literally true, or can the compiler optimize out unnecessary data copying (and the quote above just means that the behavior is equivalent to the behavior that would result if the object were copied)? It seems to me that if the objects are large and expensive to copy, then that pass-by-copy could hurt performance. Is it possible to improve performance by declaring a type to be mutable even if it doesn’t need to be, just to avoid passes by copy?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [September 20, 2017, 11:50pm UTC](https://discourse.julialang.org/t/are-immutable-types-always-passed-by-copying/6013/2 "2017-09-20T23:50:39Z")

</div>

The compiler does aggressive copy propagation (and the calling convention passes pointers to stack slots). Additionally, the compiler can in general take better advantage of immutable data. I’d always prefer an immutable where it makes semantic sense.
