# Type Checking Structs

**URL:** <https://forum.elixirforum.com/t/type-checking-structs/729>\
**Category:** Questions\
**Tags:** data-structures\
**Created:** [May 27, 2016, 11:00am UTC](https://forum.elixirforum.com/t/type-checking-structs/729 "2016-05-27T11:00:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmport80](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mmport80/32/8995_2.png) [@mmport80](https://forum.elixirforum.com/u/mmport80)\
**Post date:** [May 27, 2016, 11:00am UTC](https://forum.elixirforum.com/t/type-checking-structs/729/1 "2016-05-27T11:00:27Z")

</div>

Structs’ success typing is the same as common garden maps to Dialyzer, right?

What are the preferred ways to enforce a particular struct?

---

<div class="post-metadata">

**Author:** ![easco](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/easco/32/1250_2.png) [@easco](https://forum.elixirforum.com/u/easco)\
**Post date:** [May 27, 2016, 9:02pm UTC](https://forum.elixirforum.com/t/type-checking-structs/729/2 "2016-05-27T21:02:24Z")

</div>

In Elixir you can pattern match on the struct’s type so for example:

```
defmodule StructTest do
    defstruct name: "Foo!"
end

defmodule TestStruct do
    def takesStruct(struct = %StructTest{}) do
        IO.puts(inspect struct)
    end
end

```

The takesStruct function will only match for instances of the %StructTest{} structure.

---

<div class="post-metadata">

**Author:** ![Qqwy](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/32/1349_2.png) [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Post date:** [May 28, 2016, 11:39am UTC](https://forum.elixirforum.com/t/type-checking-structs/729/3 "2016-05-28T11:39:41Z")

</div>

You can see more information [at the Typespecs page in the Documentation](https://hexdocs.pm/elixir/typespecs.html#content).  
To match a Struct inside a typespec, use the normal `%StructName{}` syntax.

_EDIT:_ The documentation moved to a new location.

---

<div class="post-metadata">

**Author:** ![mmport80](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mmport80/32/8995_2.png) [@mmport80](https://forum.elixirforum.com/u/mmport80)\
**Post date:** [May 30, 2016, 8:42am UTC](https://forum.elixirforum.com/t/type-checking-structs/729/4 "2016-05-30T08:42:32Z")

</div>

As far as I can see Dialyzer likens structs to any general map %{}, which means you lose the specificity…

---

<div class="post-metadata">

**Author:** ![michalmuskala](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/32/20288_2.png) [@michalmuskala](https://forum.elixirforum.com/u/michalmuskala)\
**Post date:** [May 30, 2016, 1:57pm UTC](https://forum.elixirforum.com/t/type-checking-structs/729/5 "2016-05-30T13:57:18Z")

</div>

Erlang 19 introduces more advanced support from dialyzer for maps -  
checking specific keys and general key types. Support for those new  
features is in Elixir 1.3 as well. Here’s the PR that introduced them:  
[https://github.com/elixir-lang/elixir/pull/4662](https://github.com/elixir-lang/elixir/pull/4662)

---

<div class="post-metadata">

**Author:** ![mmport80](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mmport80/32/8995_2.png) [@mmport80](https://forum.elixirforum.com/u/mmport80)\
**Post date:** [May 30, 2016, 2:01pm UTC](https://forum.elixirforum.com/t/type-checking-structs/729/6 "2016-05-30T14:01:37Z")

</div>

oh really? this is very nice. when’s the eta? (if there is one)

---

<div class="post-metadata">

**Author:** ![hubertlepicki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hubertlepicki/32/6822_2.png) [@hubertlepicki](https://forum.elixirforum.com/u/hubertlepicki)\
**Post date:** [May 30, 2016, 2:16pm UTC](https://forum.elixirforum.com/t/type-checking-structs/729/7 "2016-05-30T14:16:55Z")

</div>

1.3 rc0 was just released today.

---

<div class="post-metadata">

**Author:** ![michalmuskala](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/32/20288_2.png) [@michalmuskala](https://forum.elixirforum.com/u/michalmuskala)\
**Post date:** [May 30, 2016, 3:08pm UTC](https://forum.elixirforum.com/t/type-checking-structs/729/8 "2016-05-30T15:08:26Z")

</div>

Erlang 19-rc was released couple days ago. I’d expect final release  
probably in the matter of next couple weeks.

---

<div class="post-metadata">

**Author:** ![miguelsaddress](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/miguelsaddress/32/7677_2.png) [@miguelsaddress](https://forum.elixirforum.com/u/miguelsaddress)\
**Post date:** [January 17, 2018, 1:02pm UTC](https://forum.elixirforum.com/t/type-checking-structs/729/9 "2018-01-17T13:02:51Z")

</div>

Hello!  
I am totally new to Elixir and I just found your response because I am trying to figure out this pattern matching for types

is `def takesStruct(struct = %StructTest{}) do` equivalent to `def takesStruct(%StructTest{} = struct) do`?

As I understand, `def takesStruct(%StructTest{} = struct) do` is checking that struct is of type StructTest, am I right? or is it explicitly requiring an empty struct? (I dont think it is even possible…)

Thank you in advance

---

<div class="post-metadata">

**Author:** ![amnu3387](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@amnu3387](https://forum.elixirforum.com/u/amnu3387)\
**Post date:** [January 17, 2018, 1:40pm UTC](https://forum.elixirforum.com/t/type-checking-structs/729/10 "2018-01-17T13:40:18Z")

</div>

It’s just matching on a %StructTest, empty or not, you can’t have a struct that is empty, since a struct is just a map with pre-defined fields. You can match on an empty map though, with a guard clause, `def empty(%{} = variable) when variable == %{} do` , or on empty struct fields directly `def empty(%StructTest{some_field: nil}) do`, of course if you set a default on the field it won’t work

---

<div class="post-metadata">

**Author:** ![crabonature](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/crabonature/32/7311_2.png) [@crabonature](https://forum.elixirforum.com/u/crabonature)\
**Post date:** [January 17, 2018, 1:49pm UTC](https://forum.elixirforum.com/t/type-checking-structs/729/11 "2018-01-17T13:49:00Z")

</div>

> [@miguelsaddress](#):
>
> As I understand, def takesStruct(%StructTest{} = struct) do is checking that struct is of type StructTest, am I right? or is it explicitly requiring an empty struct? (I dont think it is even possible…)

`def takesStruct(%StructTest{} = struct) do`, means:

1. this function will be invoked only when you pass to it one argument which is `%StructTest{}` struct, otherwise it will not be invoked
2. when you invoke it with proper struct, this struct will be bound to `struct` variable, which you can use later in the function body.

---

<div class="post-metadata">

**Author:** ![miguelsaddress](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/miguelsaddress/32/7677_2.png) [@miguelsaddress](https://forum.elixirforum.com/u/miguelsaddress)\
**Post date:** [January 18, 2018, 7:43am UTC](https://forum.elixirforum.com/t/type-checking-structs/729/12 "2018-01-18T07:43:27Z")

</div>

Thank you both, now it is clear 🙂
