Elixir Programming language provides a lot of beneficial and so built-in types, it’s most appropriate to define custom types when needed. Structs are continuations that are built on above maps that support compile-time checks and also provides the default values.

When to use Struct in Elixir?

Elixir Programming language provides a lot of beneficial and so built-in types, it’s most appropriate to define custom types when needed. Structs are continuations that are built on above maps that support compile-time checks and also provides the default values.

Defining Structs in Elixir

To define how a struct look like is that, PFB the defstruct construct is −

defmodule User do
defstruct name: “Harry”, age: 28
end

Used defstruct defines what are the fields that struct will possess along with their default values from the implemented keyword list. Structs take the name of the module that they possess and informed in. In the example we derive from the above structure which is mentioned below is that we characterise a struct named User. We can also now easily simply develop User structs by implementing a syntax similar to the one used to develop maps −
So, what’s this pipe operator? In OO based programming, many of us familiar with the term chaining, to do chanining in Erlang, one has to create n number of variables and associate them with values, but in Elixir, it’s simple:

new_harry = %User{ })
parthi = %User{name: “Parthi”, age: 30″}
arnold = %User{ name: “Arnold)”}

The above code will produce three different models of structs with values −

%User{age: 28, name: “Harry”}
%User{age: 30, name: “Parthi”}
%User{age: 28, name: “Arnold”}

Structs give us a compile-time that certificates that only the fields defined through defstruct will be only be concede to exist in a struct. So you cannot characterize your own fields once you have developed the struct in the module.

Infiltrating and Modernizing Structs

When we have debated on maps, we showed how we can access and get the latest status on the fields of a map. The same methodology applies to structs as well. For example, if we want to get the status on the user we created in the earlier example, then −

defmodule User do
defstruct name: “john”, age: 27
end
harry = %User {}
#harry right now is: %User {age:28, name: “Harry”}
#To accress name and age of Harry,
IO.puts(harry.name)
IO.puts(harry.age)

When the above program is in run, it creates the following result −

Harry28

To define a value in a struct, we will have to again use the same methodology that we used in the map from the earlier chapter,
Arne = %{Harry | name: “Arne”}
Do you know that Structs can also be used in pattern matching and also both for matching on the value of specific keys as well as for guarantee that the matching value in the struct of the similar category as the matched value.
‘Struct is built on top of the map and it is providing compile-time checks and default values’. So, from this sentence we know Struct is data structure. Then why we use struct instead of using map, lists, tuples, etc.,
Structs are well self-documented and are defined within modules. If you’ll give the config and doc comments to a Struct, you’ll get well documented data structure with expected behaviour, you’ll know what to expect from this data structure . But the map has dynamic behaviour so that we don’t know what values are defined dynamically. We are not able to add new behaviour to struct dynamically

Memory Sharing

Compile-time checks