Hello,
It seems that any LiveView sigil is expected to have in its scope an assigns map.
So not a keyword list or anything else, but a map.
But often I end up with a Dialyzer error (or warning) that The pattern can never match the type
Consider the following simple example for a helper:
defp product_link(product) do
assigns = %{name: product.name, url: product.url}
~L"""
<a href="<%= @url %>">
<%= @name %>
</a>
"""
end
And calling simply like that:
<%= product_link(product) %>
With ElixirLS Dialyzer, I got the following warning/error:
The pattern can never match the type.
Pattern:
%{:__changed__ => _}
Type:
%{:name => _, :url => _}
Now if instead I do the following, I don’t get the error but it’s more verbose on the calling side (particularly bothering when I have a list with more than two keywords pair).
defp product_link(assigns) do
assigns = Enum.into(assigns, %{})
~L"""
<a href="<%= @url %>">
<%= @name %>
</a>
"""
end
And calling like that:
<%= product_link(name: product.name, url: product.url) %>
But in this case I don’t get any warning/error.
So I wanted to know first, what exactly is the meaning of that warning?
Where does it come from?
Is it possible to fix it?
I’m not using any spec, but does defining some specs could prevent this error? (Though, it’s a private function so I’m not sure for that).
If not, how to disable only that warning/error here?
I didn’t defined anything particular to use Dialyzer, but it came for free with ElixrLS in VSCode. So in the meantime to learn more about specs, I rather want to stick with this default usage and try to get no warnings/errors.
And lastly, besides the Dialyzer warning, what is the best way to handle the assigns in LiveView sigils?
Thank you very much folks!