OmanF
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.
Trending in Chat/Questions
HI
Its matin and new in Elixir (my pleasure)
im creator of Land of Rust
want to build something like land of rust but for Elixir. ne...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance












Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
defstructtakes 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_keysmodule attribute.You can use
@typeto indicate what the type should be for use with dialyzer. See this and this for the limitations of success typing.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
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
Accessfunctions should be used (likeput_inin your code).IvanR
Elixir is dynamically-typed. However, with a library like Domo, it’s possible to validate struct’s type explicitly as the following: