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.

Last Post!

yevhene

yevhene

If you want to treat entities the same despite slug you can use:

  def equal?(value1, value2) do
    with {:ok, integer1} <- cast(value1),
         {:ok, integer2} <- cast(value2) do
      integer1 == integer2
    else
      _ -> false
    end
  end

I am not sure about embedding, if you want a type to behave perfectly the same as id maybe you’ll need to use:

  def dump(binary) when is_binary(binary) do
    cast(binary)
  end

  def embed_as(_format) do
    :dump
  end

Where Next?

Popular in Questions Top

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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
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
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement