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…