axelson

axelson

Scenic Core Team

Weird/incorrect output from a with statement

This is a question that originated on the Elixir Slack from David Billskog and I have made only minor changes to the module.

Given this module:

defmodule Dummy do
  def execute(x) do
    with var_a <- get_a(x),
         var_b <- get_b(x) do
      IO.inspect(var_a, label: "a")
      IO.inspect(var_b, label: "b")
      IO.inspect({var_a, var_b})

      if var_a != var_b do
        IO.inspect(var_a, label: "a")
        IO.inspect(var_b, label: "b")
        IO.inspect({var_a, var_b})
      end
    end
  end
  defp get_a(x) do
    IO.puts("get a")
    if Enum.any?(x, & &1 == "A"), do: 3, else: 2
    |> IO.inspect(label: "returning a")
  end
  defp get_b(x) do
    IO.puts("get b")
    if Enum.any?(x, & &1 == "B"), do: 2, else: 1
    #|> IO.inspect(label: "returning b")
  end
end

Dummy.execute(["A", "B"])

I get this unexpected output:

$ elixir sample.exs
get a
get b
a: 3
b: 2
{3, 2}
a: 3
b: 1
{3, 1}

Does anyone understand what is happening here? I don’t expect for the value of b to change within the if statement, and I do expect the returning a inspect to be printed. Tested on elixir 1.11.2-otp-23 and erlang 23.0.2.

Marked As Solved

josevalim

josevalim

Creator of Elixir

Thanks for the isolated case. I was able to convert it to Erlang:

-module(dummy).

-compile([no_auto_import]).

-export([execute/1]).

execute(_x@1) ->
    _a@1 = get_a(_x@1),
    _b@1 = get_b(_x@1),
    erlang:display(_b@1),
    case _a@1 /= _b@1 of
        false ->
            nil;
        true ->
            erlang:display(_b@1),
            ok
    end.

get_a(_x@1) ->
    case _x@1 == <<"A">> of
        false ->
            2;
        true ->
            3
    end.

get_b(_x@1) ->
    case _x@1 == <<"A">> of
        false ->
            1;
        true ->
            2
    end.


And I see the same issue. I will open up a bug report.

Done: https://bugs.erlang.org/browse/ERL-1440

Also Liked

blackode

blackode

We can check how the code was developed by using quote do

quote do
  if Enum.any?(x, & &1 == "A"), do: 3, else: 2
  |> IO.inspect(label: "returning a")
end
|> Macro.to_string()
|> IO.puts()

That results to

if(Enum.any?(x, &(&1 == "A"))) do
  3
else
  2 |> IO.inspect(label: "returning a")
end

Which is always true in this case. So, it won’t enter into else

axelson

axelson

Scenic Core Team

I’m still seeing the issue with elixir 1.11.2-otp-23 and erlang 23.1.5

al2o3cr

al2o3cr

Can confirm - Axelson’s example works fine on my machine (OTP23.1 / Elixir 1.11.2) and this one fails.

A colleague tried @LostKobrakai’s example on 22.2.6 / 1.11.1 and it printed 2 three times, as expected…

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
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

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement