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
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_valuesis 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?