OmanF

OmanF

Structs aren't enforced

In the attached screenshot (taken from an exercise in “Programming Elixir”) can be shown that BugReport is defined as a struct that, itself, has as a value for one of its keys a struct: Customer.

Yet, when I tried, in iex, both the put_in function and its expanded form, when the owner key received a single string, succeeded.

My understanding of structs is that once I defined BugReport to take a Customer as the type of the key owner, it set in stone.

So, what am I misunderstanding about defining structs?

Thanks.

Most Liked

peerreynders

peerreynders

defmodule BugReport do
  defstruct owner: %Customer{}, details: "", severity: 1
end

defstruct takes a keyword list. The keys describe the keys for the struct the values are the default values when the struct is created. The type of a value under a key isn’t fixed (Elixir is dynamically typed). Required keys are identified with the @enforce_keys module attribute.

You can use @type to indicate what the type should be for use with dialyzer. See this and this for the limitations of success typing.

michalmuskala

michalmuskala

Why should it fail? Elixir is a dynamically typed language - there are generally no type constraints. The values in struct declaration are about a default value when a new struct is created - they don’t mean anything more and in particular don’t affect how struct can be updated.

dimitarvp

dimitarvp

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).

Where Next?

Popular in Chat/Questions Top

Fl4m3Ph03n1x
Background I have been reading quite a lot about design and architecture in Elixir and overall FP. My latest incursions took to me the On...
New
ericdouglas
I think that would be really interesting to have official books created by the community about all kinds of development we can do with El...
New
New
OmanF
In the attached screenshot (taken from an exercise in “Programming Elixir”) can be shown that BugReport is defined as a struct that, itse...
New
nur
https://e.planaria.network Build a NoSQL DB, Build a Relational SQL Database, Build a Graph Database, Build a File System, Build a Bitc...
New
makeitrein
More Ecto questions! More madness! The context: there’s a list of books that I want to filter with a dropdown… The dropdown: looks some...
New
Kielo
Hi, I run a language learning blog and would like to learn how to code so I can create an app to help English speakers learn French. I ...
New
koen_vb
Hi, I was looking for a pointer of how I could most easily start with phoenix regarding deploying it to something like linode or google c...
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
venomnert
Background I have been a backend elixir developer for about 3 years now. I have been mainly working on simple CRUD applications. Context...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
New

We're in Beta

About us Mission Statement