elixirelixir-iex

Why Do I See Two Results?


I'm working through Sasa Juric's "Elixir In Action" and I spotted something I find a little bit puzzling. I was working through the examples he gives around page 140 or so and I saw this:

iex(2)> send(self, {:message, 1})    # Line 1
{:message, 1}
iex(3)> receive_result = receive do
...(3)>   {:message, x} -> x + 2
...(3)> end
3
iex(4)> IO.inspect receive_result
3
3

Why does IO.inspect print the value twice? Is it related to the send message on Line 1 immediately displaying the tuple?


Solution

  • This is just how IO.inspect/2 works in iex:

    iex(10)> IO.inspect(3)
    3 # From IO.inspect
    3 # return value in iex
    

    This happens since IO.inspect prints the value and returns the same value. IO.puts/2 prints the value and returns :ok

    iex(11)> IO.puts(3)  
    3    # From IO.puts
    :ok  # return value in iex