# PSA: New keywords for defining types

**URL:** <https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029>\
**Category:** Internals & Design\
**Created:** [February 10, 2017, 6:28pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029 "2017-02-10T18:28:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [February 10, 2017, 6:28pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/1 "2017-02-10T18:28:13Z")

</div>

In a rather disruptive, but I think worthwhile, change, we now have different keywords for defining types. Here’s a quick summary:

- `immutable` changes to `struct`
- `type` changes to `mutable struct`
- `abstract` changes to `abstract type ... end`
- `bitstype 32 Char` changes to `primitive type Char 32 end`

The PR that did this: [https://github.com/JuliaLang/julia/pull/20418](https://github.com/JuliaLang/julia/pull/20418)  
Kristoffer has a helpful script for the `struct` part of the change here: [https://github.com/JuliaLang/julia/pull/20418#issuecomment-277201589](https://github.com/JuliaLang/julia/pull/20418#issuecomment-277201589)  
Hopefully it can be extended to handle abstract and primitive as well.

For now, `type` and `immutable` are still allowed without a deprecation warning, since Compat.jl won’t be able to support the new syntax.

Using `type ... end` allows Compat to support the new abstract & primitive type syntax, and also allows us future flexibility to add things inside such definitions. The extra verbosity is considered tolerable since these keywords are used much less often than the `struct` keywords. It also means that in 1.0, the words `abstract`, `type`, `immutable`, `mutable`, and `primitive` will be usable as normal identifiers. The only type-related word that will actually be reserved is `struct`.

This change allows us to use clearer terminology, where keywords correspond to descriptive terms where applicable:

primitive type: a scalar-like type that’s just a series of bits, no fields  
struct: data consisting of named fields  
(im)mutable type: a type that can or can’t be mutated  
bits type: pointer-free, immutable data (might be either struct or primitive)  
type: something that describes a class of values

The preferred default user-defined type is `struct`. No more telling people to go through and make everything immutable for performance 🙂

-Jeff

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [February 10, 2017, 6:55pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/2 "2017-02-10T18:55:18Z")

</div>

> [@jeff.bezanson](#):
>
> Kristoffer has a helpful script for the struct part of the change here: [https://github.com/JuliaLang/julia/pull/20418#issuecomment-277201589](https://github.com/JuliaLang/julia/pull/20418#issuecomment-277201589)

Can we have such a script that corrects all the Julia material out there on the web? 🙂

---

<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:** [February 10, 2017, 7:12pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/3 "2017-02-10T19:12:20Z")

</div>

With the changes this will do to the parser, does this mean one can define macros like

```julia
@inherits interface type
  # does stuff
end

or will only specific `keyword type` phrases parse correctly?
```

---

<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:** [February 10, 2017, 7:22pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/4 "2017-02-10T19:22:21Z")

</div>

> [@jeff.bezanson](#):
>
> The preferred default user-defined type is struct. No more telling people to go through and make everything immutable for performance

Can you please elaborate on this? Does this mean that the change is not merely syntactic, but the semantics also changed, and `struct` is now faster than it used to be?

---

<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:** [February 10, 2017, 7:30pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/5 "2017-02-10T19:30:17Z")

</div>

This should parse on old julia versions too.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 10, 2017, 7:32pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/6 "2017-02-10T19:32:35Z")

</div>

It’s just that the easiest and most obvious thing to use used to be `type`. That’s now referred to as a `mutable struct`. If you don’t need mutable behaviors, though, you really should have been using `immutable`. That’s now just `struct`.

Now it’s most likely that folks will use `struct` first, and only use `mutable` when they actually need a mutable structure.

---

<div class="post-metadata">

**Author:** ![turtle](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@turtle](https://discourse.julialang.org/u/turtle)\
**Post date:** [February 10, 2017, 7:50pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/7 "2017-02-10T19:50:09Z")

</div>

I think `immutable` and `mutable` would have been  
much better choices than `struct` and `mutable struct`.

Single word keywords and only a single change `type` → `mutable`.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [February 10, 2017, 7:54pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/8 "2017-02-10T19:54:31Z")

</div>

@ChrisRackauckas Only specific `word type` keywords will have this parsing.

@turtle Yes that would have been a slight improvement over what we had before, but we would then still lack a descriptive noun for this kind of type. `primitive` types and tuples are also immutable, so it would still be a bit unclear. It’s also a bonus to avoid stealing the words `abstract` and `primitive`.

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [February 10, 2017, 8:50pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/9 "2017-02-10T20:50:03Z")

</div>

Naturally it is really hard to form an unbiased opinion on such a change after months of getting used to the current syntax. What I can say is that I am glad these kind of changes are done sooner rather than later though. Thanks to everyone putting so much thought into these aspects

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 11, 2017, 12:06am UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/10 "2017-02-11T00:06:36Z")

</div>

I don’t like suggesting this without volunteering code, but would it make sense to take this opportunity to fix the singleton inconsistency?

```julia
type X end
X() === X() # true

```

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [February 11, 2017, 12:27am UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/11 "2017-02-11T00:27:40Z")

</div>

That would be a rather unrelated semantic change; this was just a renaming.

Whether this is inconsistent is debatable. There is no way to tell `X()` and `X()` apart aside from `===` itself, so `===` giving either true or false is equally consistent with the rest of the language.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 11, 2017, 1:10am UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/12 "2017-02-11T01:10:45Z")

</div>

> [@jeff.bezanson](#):
>
> There is no way to tell X() and X() apart aside from === itself,

That’s also [true](https://github.com/JuliaLang/julia/issues/17149) of a mutable type with a single `::Void` field, but its instances are `!==`

---

<div class="post-metadata">

**Author:** ![bpr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bpr/32/2710_2.png) [@bpr](https://discourse.julialang.org/u/bpr)\
**Post date:** [February 11, 2017, 1:13am UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/13 "2017-02-11T01:13:19Z")

</div>

> [@jeff.bezanson](#):
>
> This change allows us to use clearer terminology, where keywords correspond to descriptive terms where applicable:

Given that, what’s the rationale for using `function` as a keyword rather than `method`?

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [February 11, 2017, 3:14am UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/14 "2017-02-11T03:14:35Z")

</div>

> [@cstjean](#):
>
> mutable type with a single ::Void field, but its instances are !==

Yes, you’re right. We could extend the current behavior to any struct whose size is 0. Either that or always making distinguishable instances, as you suggest, would be defensible.

> [@bpr](#):
>
> Given that, what’s the rationale for using function as a keyword rather than method?

It’s subjective, but I feel this is not so much of a problem. It might be because methods are not really first class. Typical code doesn’t directly manipulate methods, so that confusion doesn’t come up. The `function` keyword is great when a function has one definition. But many people are also used to the idea that a function can have multiple definitions, as in C++, so using the same syntax for each definition is not so bad either.

---

<div class="post-metadata">

**Author:** ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)\
**Post date:** [February 11, 2017, 8:54am UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/15 "2017-02-11T08:54:06Z")

</div>

> [@jeff.bezanson](#):
>
> primitive type: a scalar-like type that’s just a series of bits, no fieldsstruct: data consisting of named fields(im)mutable type: a type that can or can’t be mutatedbits type: pointer-free, immutable data (might be either struct or primitive)type: something that describes a class of values

Wouldn’t “value type’” be a more standard name for “bits type”? Meaning also a type that is allocated on the stack rather than the heap. At least that’s the terminology that’s used wen discussing such things in Scala, C# (structs) or Java 10.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 11, 2017, 4:49pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/16 "2017-02-11T16:49:27Z")

</div>

> [@Steven\_Sagaert](#):
>
> Wouldn’t “value type’” be a more standard name for “bits type”? Meaning also a type that is allocated on the stack rather than the heap.

No, because `struct` (formerly `immutable`) is also a value type and can also be stack-allocated. (And these types may not go on the stack, either—the compiler may put them in registers.) Also, I’m skeptical of naming types based on implementation details. Especially since compiler optimizations may be implemented in the future to stack-allocate other kinds of objects (e.g. [https://github.com/JuliaLang/julia/pull/12205](https://github.com/JuliaLang/julia/pull/12205)).

---

<div class="post-metadata">

**Author:** ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)\
**Post date:** [February 11, 2017, 5:25pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/17 "2017-02-11T17:25:53Z")

</div>

yes but int he definition that Jeff gave that I quoted bits type is both primitive types & struct. So if you say no because struct is also a value type you just confirmed my point.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [February 11, 2017, 6:46pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/18 "2017-02-11T18:46:33Z")

</div>

“bits type” also means that the object does not contain any pointers. That’s orthogonal to how it is allocated or how it is passed. It means, for example, that you can copy the object into a shared memory segment and have things work.

---

<div class="post-metadata">

**Author:** ![turtle](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@turtle](https://discourse.julialang.org/u/turtle)\
**Post date:** [February 12, 2017, 9:54am UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/19 "2017-02-12T09:54:47Z")

</div>

OK, so the noun `type` is replaced by the noun `struct` for composite types.

I would still prefer less typing.  
As far as I remember `immutable` was just an abbreviation for `immutable type`.  
Would it be an option to use `mutable` as an abbreviation for `mutable struct` ?

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [February 12, 2017, 12:37pm UTC](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029/20 "2017-02-12T12:37:52Z")

</div>

> [@turtle](#):
>
> noun type is replaced by the noun struct for composite types

`type` is replaced by `mutable struct`, to be exact.

> [@turtle](#):
>
> As far as I remember `immutable` was just an abbreviation for `immutable type`.

Semantically - yes, but syntactically `immutable type` is invalid, at least in Julia 0.5.

[Next page](https://discourse.julialang.org/t/psa-new-keywords-for-defining-types/2029.md?page=2)
