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 −


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:

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

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 −

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

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,

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

 

Leave Comment

Your email address will not be published. Required fields are marked *