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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
I’m using an Umbrella project for a Phoenix application, and I want to have one Ecto Repo and one PostgreSQL database shared by all apps....
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #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.