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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
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
Blokh
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
roeland
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
subsaharancoder
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
mohsen
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
jaybe78
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 Top

JesseHerrick
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews