In my previous post https://medium.com/@cowen/annotations-in-elixir-450015ecdd97 I discussed how we can use Elixirs rich macro system to bring annotations to elixir.
In this post lets bring out the macro toolbelt once again to look at simplifying converting raw elixir maps to well defined structs.
There is often a debate in the mind of any elixir developer as to when to use Structs and when to stick with basic data types such as maps and lists. The general ethos I feel works is that if the data you are working with has a known structure in the real world then it makes sense to get this data in to a Struct as early as possible.
Structs have a few advantages over unstructured data, namely:
- Structs are defined in modules with the same name, as such methods that related to the structure can be logically grouped together here
- Structs allow more rigid pattern matching captures and when paired with @spec annotations give the user a clear understanding of what the method expects to take in.
On top of this Elixir Struct can be treated very much like just regular maps with additional metadata in some of their βprivateβ keys. So you donβt really loose any of the flexibility of pattern matching and language support.
continue reading on medium.com
β οΈ This post links to an external website. β οΈ
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.