jejuro

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,

  1. 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}
  1. 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?
  2. 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?
  3. 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.

Showing Posts 1 to 7

LostKobrakai

LostKobrakai

Let’s do this from the outside in:

changeset/2 has 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 an Ecto.Changeset struct.

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, but Ecto.Changeset.cast’s typespec accepts %{binary => term} | %{atom => term} | :invalid, which means either the atom :invalid or 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

jejuro OP

Hi LostKobrakai. I have thankfully read several articles of you written before.

  1. The first parameter of the changeset function is the person struct, and the second is a map. Can the type of the person struct be denoted as {map, map}?
  2. What is the meaning of the two lowdash ‘__’ before and after some keys, such as __struct__ and __changeset__?
jejuro

jejuro OP

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

Aetherus

The first parameter of the changeset function is the person struct, and the second is a map . Can the type of the person struct be denoted as {map, map}?

No. I guess the person struct is something like

%MyApp.Person{
  name: "John Doe",
  gender: "male"
}

In Elixir, this is just

%{
  __struct__: MyApp.Person,
  name: "John Doe",
  gender: "male"
}

or to be more bare metal,

%{
  :__struct__ => :"Elixir.MyApp.Person",
  :name => "John Doe",
  :gender => "male"
}

So, it matches %{:__struct__ => atom, optional(atom) => any}.

What is the meaning of the two lowdash ‘__’ before and after some keys, such as __struct__ and __changeset__ ?

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

jejuro OP

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

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 (both data() and types() are just aliases to map() in Ecto code).

jejuro

jejuro OP

Thank you, Nicd.
Much more clear than before.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
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
peck
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
achenet
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
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
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
Alvinkariuki
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 Top

budgie
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
KristerV
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
mudasobwa
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
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
budgie
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews