Fl4m3Ph03n1x

Fl4m3Ph03n1x

Can MapSet be an attribute?

Background

I have a function that works with MapSets, and I have a set of values (static values) that I need to pass it.
So I have decided to put these values into an attribute, however dialzyer complains and I don’t get why.

Code

defmodule MyApp.Test do

  alias MyApp.MyDependency

  @data MapSet.new(["a", "b", "c"])

  def do_something_with_data(request), do: MyDependency.validate(request, @data)
end

This code gives me the following error:

lib/myapp/test.ex:5: The call ‘Elixir.MyApp.MyDependency’:validate(data@1::any(),#{‘struct’:=‘Elixir.MapSet’, ‘map’:=#{<<:56,:*32>>=>}, ‘version’:=2}) does not have an opaque term of type ‘Elixir.MapSet’:t(_) as 2nd argument

Which is rather confusing.

Why does this work?

I have battled with MapSets before, namely with this opaque value issue. So I tried to trick dialyzer and created the following code, which works:

defmodule MyApp.Test do

  alias MyApp.MyDependency

  @data ["a", "b", "c"]

  def do_something_with_data(request), do: MyDependency.validate(request,  MapSet.new(@data))
end

Questions

Why doesn’t the first example work? What am I missing?
Why does the second one work?
Does it have something to do with how the compiler parses the files?

Marked As Solved

LostKobrakai

LostKobrakai

Dialyzer essentially sees this code:

MyDependency.validate(request, %MapSet{map: %{"a" => [], "b" => [], "c" => []}, version: 2})

While this is the same data, this is not like you’re supposed to create opaque types.

The reason for that is that a module attribute does just store plain data at compile time, injecting it everywhere it’s read. Dialyzer being an erlang tool has no idea there even are module attributes, so it cannot track the type from the compile time initialization to being injected, to being used at runtime.

Also Liked

mindok

mindok

There’s a thread on disabling warnings here that may give you some hints…

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
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
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
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
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement