ken-kost

ken-kost

Elixir v1.19 then compile warning

Following this and I’m wondering the deets about it. :nerd_face:

I made a simple recreation:

  def hello(map) do
    map
    |> Map.to_list()
    |> Enum.reduce_while(%{}, fn {key, value}, acc ->
      if value == 2 do
        {:halt, :error}
      else
        {:cont, Map.put(acc, key, value)}
      end
    end)
    |> then(fn
      %{a: a} = data when a == 3 ->
        %{data | b: a}

      data ->
        data
    end)
    |> case do
      :error -> "It's error"
      data -> "It's okay #{data.a}"
    end
  end

This produces the warning:

    warning: the following clause will never match:

        :error

    because it attempts to match on the result of:

        (fn
           %{a: a} = data when a == 3 -> %{data | b: a}
           data -> data
         end).(
          Enum.reduce_while(Map.to_list(map), %{}, fn {key, value}, acc ->
            if value == 2 do
              {:halt, :error}
            else
              {:cont, Map.put(acc, key, value)}
            end
          end)
        )

    which has type:

        dynamic(%{..., a: term(), b: term()})

    typing violation found at:
    │
 33 │       :error -> "It's error"
    │       ~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/then_case.ex:33: ThenCase.hello/1

It’s like putting the first clause in then makes the compiler think the data variable is always dynamic(%{..., a: term(), b: term()}).

Removing that clause or moving it inside case (inside then or removing then and just case) resolves the warning.

It seems the compiler does not like cases made with then. :smiley:
I’m just curious, is this the expected behavior and what’s the reason behind it? :bug:

Marked As Solved

josevalim

josevalim

Creator of Elixir

That’s a bug in the type system, I will take it from here. Thank you!

Where Next?

Popular in Questions Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

We're in Beta

About us Mission Statement