chocolatedonut

chocolatedonut

Can this error be caught by a tool (mix compile, Dialyzer, etc.)?

TL;DR
A single element, in this case an integer, is given to a Phoenix.Component but a list of such elements should be given. Can a compile-time warning/error be produced by any tool?

I’ve made a repro project. The gist of it is that

  1. A.Foo returns an integer to
  2. AWeb.ALive and it in turn (incorrectly) forwards this integer (rather than a list of integers) to
  3. AWeb.AComponent.
#1
defmodule A.Foo do
  @spec number() :: pos_integer()
  def number() do
    1729
  end
end

#2
defmodule AWeb.ALive do
  use AWeb, :live_view

  def mount(_params, _session, socket) do
    {:ok,
     socket
     |> assign(:number, A.Foo.number())}
  end

  def render(assigns) do
    ~H"""
    <AWeb.AComponent.bar numbers={@number} />  # Should've been `[@number]`
    """
  end
end

#3
defmodule AWeb.AComponent do
  use Phoenix.Component

  attr(:numbers, :list, required: true)

  @spec bar(%{numbers: list(pos_integer)}) :: any
  def bar(assigns) do
    ~H"""
    <li :for={number <- @numbers}>
      <%= number %>
    </li>
    """
  end
end

Can this be catched by the compiler, Dialyzer, anything? My understanding is “no”, but I hope I’m wrong:

  • attr macro atm only does compile-time validation of literals
  • even @specing the Phoenix.Component above with @spec bar(%{numbers: list(pos_integer)}) :: any doesn’t help, as mix dialyzer gives no warnings.

Is there any other tool that would help with this?

Most Liked

jeremyjh

jeremyjh

I haven’t looked at the implementation of the heex compiler, but my intuition suggests there is a runtime apply happening versus compiling a direct invocation of the function in the component syntax. If that is the case then there is no static link between the bar function and its invocation. I do not know if that is a detail that can be changed, but I’d start there and at least rule out the possibility that could be done to support dialyzer.

Where Next?

Popular in Questions Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
New
Kagamiiiii
Student &amp; 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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
clayschick
I'm trying to create a simple query to select distinct values from a column. If I only use select and distinct I get back a list of uniqu...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement