# Clarity on struct vs type

**URL:** <https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197>\
**Category:** New to Julia\
**Tags:** documentation\
**Created:** [January 6, 2018, 1:16pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197 "2018-01-06T13:16:17Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)\
**Post date:** [January 6, 2018, 1:16pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/1 "2018-01-06T13:16:17Z")

</div>

I’m just learning Julia, and I’m finding the docs not so clear on the differences between `type` and `struct`. In the docs there are no real examples of declaring custom types using the `type` keyword, instead there’s a section on composite types using `struct`. So I’m just pointing out that as a beginner the docs are leaving me with questions:

1. What is the difference between using `struct` and `type`, or `mutable struct` and `type`?
2. Why would you choose one over the other? Why is `struct` used in the docs rathe than `type`?

---

<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:** [January 6, 2018, 1:26pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/2 "2018-01-06T13:26:42Z")

</div>

`type` (and `immutable`) are deprecated, just use `mutable struct` (or just `struct`).

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)\
**Post date:** [January 6, 2018, 1:41pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/3 "2018-01-06T13:41:28Z")

</div>

Cool, answers my question. I wasn’t getting any deprecation warnings (using 0.6.2).

Feels a bit unnatural to me to me, personally `type` seemed more sensible, but looks like there was a long discussion about it on [#19157](https://github.com/JuliaLang/julia/issues/19157)

EDIT: Actually, being more honest, this is the first thing I’ve come across in Julia which just seems flat out ugly.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 6, 2018, 3:43pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/4 "2018-01-06T15:43:12Z")

</div>

> [@drbenvincent](#):
>
> this is the first thing I’ve come across in Julia which just seems flat out ugly

I will not comment on the aesthetic part (I happen to like `struct`), but note that “type” is used in another, very specific sense by Julia (the thing that dispatch operates on, etc), so using it for composite types was just confusing.

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)\
**Post date:** [January 6, 2018, 8:22pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/5 "2018-01-06T20:22:35Z")

</div>

Sure thing. I’m a beginner here so can’t see the bigger picture. Very much enjoying my journey though. Loving Julia so far 🙂

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [June 9, 2018, 11:37am UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/6 "2018-06-09T11:37:45Z")

</div>

Have been aware that `type` is noted to become deprecated in 0.7.0-alpha. Does this mean that an overhaul is due in the type [section](https://docs.julialang.org/en/latest/manual/types/) of the Docs?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 9, 2018, 11:48am UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/7 "2018-06-09T11:48:17Z")

</div>

AFAICT it has already been updated, I see no mention of `type` or `immutable`.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [June 9, 2018, 11:55am UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/8 "2018-06-09T11:55:10Z")

</div>

Sorry, I’m just a beginner. IIUC, the concept of `type` is not deprecated; it is only that the users shouldn’t define any type and they should use `struct` instead?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 9, 2018, 12:15pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/9 "2018-06-09T12:15:44Z")

</div>

It was just a name change, to be clearer and more consistent, nothing else changed.

type → mutable struct  
immutable → struct

(edit: it was also to make the preferred type of struct, immutable, to be the “default”)

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [June 9, 2018, 12:17pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/10 "2018-06-09T12:17:48Z")

</div>

> [@ScottPJones](#):
>
> It was just a name change, to be clearer and more consistent, nothing else changed.
> 
> type -\> mutable struct  
> immutable -\> struct

Then all the words `type`s shall be replaced by `mutable struct` in both the Docs and any actual Julia code?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 9, 2018, 12:19pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/11 "2018-06-09T12:19:42Z")

</div>

No, when the docs are talking about the concept of a type, then it doesn’t change (and this was a big part of _why_ the change was necessary, the confusion when talking about a specific kind of type, i.e. a structure that might be mutable or immutable, vs. abstract types, primitive types, etc., or talking about the concept).

In your _code_ (but not in any doc strings discussing the concept 😀), yes, replace immutable =\> struct, and type =\> mutable struct.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [June 9, 2018, 12:51pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/12 "2018-06-09T12:51:17Z")

</div>

Thanks for the clarification. (This) confusion dispelled.

Now, bear with me for slightly abusing this thread for a quick question regarding the same doc file:

The first sentence in this [section](https://docs.julialang.org/en/latest/manual/types/#Abstract-Types-1) is

> Abstract types **cannot be instantiated** , …

A few paragraphs later, it says

> When no supertype is given, the default supertype is Any – a predefined abstract type that all objects are **instances** of and all types are subtypes of.

Are they contradictory? Confused.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 9, 2018, 12:54pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/13 "2018-06-09T12:54:11Z")

</div>

OK, I see your point, that is a bit unclear.  
I believe, when it says “all objects are instances of”, simply means that the release `isa Any` is always for any object. (`isa` being true for instances of a particular type or subtype of the given type), so not really contradictory.

So, `1` is an instance of an `Int`, and it’s an instance of a `Signed` type, an `Integer` type, a `Number` type, and `Any`.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [June 9, 2018, 1:05pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/14 "2018-06-09T13:05:20Z")

</div>

Thanks! I kind of understand what you mean. So probably the second sentence can be rephrased as the following?

> When no supertype is given, the default supertype is Any – a predefined abstract type that all objects are instances **of the concrete subtypes** of and all types are subtypes of.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 9, 2018, 1:12pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/15 "2018-06-09T13:12:47Z")

</div>

I’m not the best on documentation, one of the core people might be better at a good description.  
You might want to bring it up on slack, or make a PR to change that in the documentation.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [June 9, 2018, 1:14pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/16 "2018-06-09T13:14:07Z")

</div>

Thanks, buddy!! Will raise the point on slack. Thanks!

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 9, 2018, 1:24pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/17 "2018-06-09T13:24:53Z")

</div>

I think the meaning is that `a` is an instance of a concrete type, but it is also descended of a number of abstract types.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [June 9, 2018, 2:37pm UTC](https://discourse.julialang.org/t/clarity-on-struct-vs-type/8197/18 "2018-06-09T14:37:19Z")

</div>

Yes, I second!
