jejuro
Hi, all.
I have read several elixir books and the only Ecto book, and still confused on Typespecs. I googled a few articles on typespecs including official Typespect document of Elixir site and questions in elixir forum here, but not clear at all.
For example,
defmodule Friends.Person do
use Ecto.Schema
schema "people" do
field(:first_name, :string)
field(:last_name, :string)
field(:age, :integer)
end
@spec changeset(
{map, map} | %{:__struct__ => atom | %{__changeset__: map}, optional(atom) => any},
:invalid | %{optional(:__struct__) => none, optional(atom | binary) => any}
) :: Ecto.Changeset.t()
def changeset(person, params \\ %{}) do
person
|> Ecto.Changeset.cast(params, [:first_name, :last_name, :age])
|> Ecto.Changeset.validate_required([:first_name, :last_name])
end
end
I hope someone explain the typespec of the changeset function above.
I guess,
- The paremeters of the changeset function can be one of the three:
(1) {map, map},
(2) %{:__struct__ => atom | %{__changeset__: map}, optional(atom) => any}, :invalid ,
(3) %{optional(:__struct__) => none, optional(atom | binary) => any}
- In the first case(1) of {map, map}, The type of the first parameter person is a struct, and that of the second is a map. What is the meaning of {map, map} tuple? That is to say, why {} is necessary here?
- In the second case(2), the changeset function needs only two parameters, then why the third parameter, :invalid atom, is there? What is the meaning of
__strunct__in:__struct__ => atom? What is the meaning of__changeset__in%{__changeset__: map}? What is the meaning of `optional(atom) => any’ here? - What is the meaning of the third case(3)?
%{optional(:__struct__) => none, optional(atom | binary) => any}
It’s quite a verbose question, but it surely will help many novices in the future who visit this forum or googling similar questions.
Always thank you all.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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 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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
Let’s do this from the outside in:
changeset/2has an arity of 2 (ignoring the default for now, typespecs don’t know about default values) and as any function one return value.The return value is simple:
Ecto.Changeset.t, which essentially aliases anEcto.Changesetstruct.Parameter 1 is
{map, map} | %{:__struct__ => atom | %{__changeset__: map}, optional(atom) => any}So it can either be a 2 element tuple or a struct.
The tuple needs to have a map for each element. This is the format of schemaless changesets.
The schema needs to have a struct key and optionally further atom key. I’m not sure exactly what the
%{__changeset__: map}is for in the struct key. But the value shall be able to have that format as well.Parameter two is
:invalid | %{optional(:__struct__) => none, optional(atom | binary) => any}. I’m not sure why the struct key is explictly listed here, butEcto.Changeset.cast’s typespec accepts%{binary => term} | %{atom => term} | :invalid, which means either the atom:invalidor a map of either exclusively string keys or exclusively atom keys. What you’re seeing allows for the same, but with different constraints.If you got to those typespecs via the auto-generation of elixir-ls: Dialyzer sometimes does things more complex then they need to be.
jejuro
Hi LostKobrakai. I have thankfully read several articles of you written before.
__struct__and__changeset__?jejuro
Would anyone write the typespecs of the changeset function by hand without Dialyzer?
My guess is;
@spec changeset(Person.t(), %{optional(key) => value}) :: Changeset.t()Seeing the Typespecs page, the built-in type of
struct()is defined as%{:__struct__ => atom(), optional(atom()) => any()}. I hope someone explain what this means.Aetherus
No. I guess the person struct is something like
In Elixir, this is just
or to be more bare metal,
So, it matches
%{:__struct__ => atom, optional(atom) => any}.Syntactically, the double underscore is nothing special. It’s just a part of a field name. Visually, it tells you that this field is for special use (like reflection), and the Elixir language or the lib/framework you use depends on it. So, you can read it, but you should not change its value or remove the field.
jejuro
Now I understand some of the crypto codes. Would you show me the typespecs of the changeset function if you write it rather than those of Dialyzer?
And what is meaning of
{map, map}in the typespecs above?Nicd
Ecto.Changeset.cast/4 has its first argument typed as
Ecto.Schema.t() | t() | {data(), types()}. So the{map, map}is coming from that last argument type, which is a tuple of two maps (bothdata()andtypes()are just aliases tomap()in Ecto code).jejuro
Thank you, Nicd.
Much more clear than before.