Sanjibukai

Sanjibukai

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..

Showing Posts 1 to 3

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.

Orzech

Orzech

What is the right implementation for these two callbacks, given the aforementioned example?

Currently I have:

def embed_as(_format) do
  :self
end

def equal?(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
  integer1 == integer2
end

But perhaps equal?/2 does not need or even should not have is_integer guards?

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews