Structs aren't enforced

As others mentioned, Elixir is dynamically-typed.

However, you can define the structures as Ecto schemata and enforce types via the Changeset functions – even without using a database (many people wrongly assume using Ecto mandates a database). This thread has pointers on how to use Ecto without a database – maybe even better resources exist.

You can even use plain structures without Ecto wiring, as long as you pass in typing information when assigning struct fields.

Have in mind though, if you are gonna use Ecto’s Changeset, that should be the only way you manipulate the structures defined through it. No normal assigments or Access functions should be used (like put_in in your code).

3 Likes