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

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

Other popular topics Top

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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement