Sanjibukai

Sanjibukai

Explanatinos about `use Ecto.Type` and `@behaviour Ecto.Type`

Hi everybody,
I’m following along the Programming Phoenix 1.4 book and at a moment we are implementing a custom Ecto type.
According to the docs (here in the second line) it is also expected that 4 functions are also defined.

Here is the extract:

#---
# Excerpted from "Programming Phoenix 1.4",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/phoenix14 for more book information.
#---
defmodule Rumbl.Multimedia.Permalink do
  @behaviour Ecto.Type

  def type, do: :id

  def cast(binary) when is_binary(binary) do
    case Integer.parse(binary) do
      {int, _} when int > 0 -> {:ok, int}
      _ -> :error
    end
  end

  def cast(integer) when is_integer(integer) do
    {:ok, integer}
  end

  def cast(_) do
    :error
  end

  def dump(integer) when is_integer(integer) do
    {:ok, integer}
  end

  def load(integer) when is_integer(integer) do
    {:ok, integer}
  end
end

As you can see the 4 functions (type, cast, dump and load) are defined and everything works as expected.

However in my case I got dialyzer warnings (I’m using ElixirLS) about two callback functions that are still not implemented: equal?/2 and embed_as/1

As my first question, what are the difference between functions and callbacks within elixir Modules (like here in Ecto.Type)?

I noticed that all the callbacks are also present in the functions (with the arity decremented by one) and so that they don’t receive the type.
Is it something comparable to static methods in OOP where there is no need to instantiate an object?

Also, I noticed that if I use Ecto.Type instead of @behaviour Ecto.Type, everything still work but I don’t have the warnings.
I took a look at the source code and it seems that using Ecto.Type automatically defines those two functions.

So my second questions is what are the difference between these two declarations:

  • @beviour Ecto.Type
  • use Ecto.Type

And why choose one over the other?

Thank you very much for any details…

Most Liked

LostKobrakai

LostKobrakai

The callbacks are functions other modules implementing the behaviour need to implement. That there are functions named the same in Ecto.Type itself is totally unrelated to the callbacks.

The two callbacks you were warned about are only recent additions to the Ecto.Type behaviour and only relevant to a subset of types. Therefore the simultaneous addition of use Ecto.Type so people can update their types without actually needing to add more code to existing Ecto.Type implementations. The macro basically adds the default implementation for those two callbacks, which constitute to the behaviour of the type before those additions. New implementations can chose to override those. When doing @behaviour Ecto.Type you’ll however need to implement all callbacks on your own, therefore the warning.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement