Softknobs

Softknobs

Can a @callback behaviour call a private function inside the module that "uses" it?

Hi,

I can’t tell if this is related to Elixir itself or the implementation of the Phoenix.LiveComponent module (from LiveView) I am using. Also, I am not familiar with Behaviours yet and new to Elixir.

In my code, I tried to refactor this code (this is a simplified example):

defmodule My.Module do
  use Phoenix.LiveComponent

# update is a behaviour (@callback) from  LiveComponent, 
# "assign" function adds values to the socket.assigns map
def update(assigns, socket) do
  socket |> assign(:value, assigns.value)
end

to this:

defmodule My.Module do
  use Phoenix.LiveComponent

# "assign" function adds values to the socket.assigns map
defp default_values(assigns, socket) do
  socket |> assign(:value, assigns.value)
end

 # update is a behaviour from  LiveComponent
def update(assigns, socket) do
  socket |> default_values(assigns)
end

The former code works, while the latter fails with an error (not Elixir) complaining that :value is not found in socket. But looking at the code, it is not true because default_values function adds :value to the socket just like the former code, socket |> assign(:value, assigns.value), did it.

Then I made default_values public instead of private, and the refactored code started working like the former version. I am a bit puzzled because if is a accessibility problem, I would have expected either:

  • a compiler error telling the behaviour cannot access a private function inside the module that uses it
  • a runtime error telling the function default_values is undefined or private.

I did not expect the code would proceed “normally” ignoring the private function (or silently catching the problem, or whatever happens here…) leading to a crash later because, in the end, the function was not applied to the data.
Actually it worries me because in this particular case the program crashes later because some requirements are not met. This not always the case, and it would have been very hard to figure out one function was not applied without crashing in other situations.

Does anybody have an idea of what is happening in this case? Does it have to do with Elixir itself or could it be the way the “used” module is implemented?

Marked As Solved

lucaong

lucaong

In other words, you might have misunderstood the pipe operator. The pipe operator performs the function call on the right side inserting the expression on the left side as the first argument.

This is equivalent to default_values(socket, assigns, socket), which won’t work because your default_values function takes only two arguments, the last of which is the socket.

The code posted above by @hauleth fixes this by defining default_values to take two arguments, the first of which is the socket. This way, the pipe operator will correctly introduce the socket argument.

Also Liked

hauleth

hauleth

I am not sure how Yours code even compile as there is no default_values/3 function. This should work as expected:

# "assign" function adds values to the socket.assigns map
defp default_values(socket, assigns) do
  socket |> assign(:value, assigns.value)
end

 # update is a behaviour from  LiveComponent
def update(assigns, socket) do
  socket |> default_values(assigns)
end
lucaong

lucaong

After the edit, your code still has the problem that you are defining default_values taking the socket as the last argument, while the pipe operator will insert it as the first.

As an additional note, you don’t necessarily have to use the pipe operator if you make a single call.

# This
foo |> bar(:something)

# Is equivalent to this
bar(foo, :something)

The situation where the pipe operator is useful is when you chain multiple calls, often transforming a data structure in a pipeline, so that your code flows in the right “direction”:

# This:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
|> Enum.filter(&Integer.is_even/1)
|> Enum.map(fn x -> x * 2 end)

# Is the same as this:
Enum.map(
  Enum.filter([1, 2, 3, 4, 5, 6, 7, 8, 9], &Integer.is_even/1),
  fn x -> x * 2 end
)

# Except the first version is more readable,
# and makes it clear that we first filter, then map
lucaong

lucaong

Welcome, and by the way, I definitely made this same mistake more frequently than I want to admit :slight_smile:

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement