为什么我看到两个结果?
Why Do I See Two Results?
我正在研究 Sasa Juric 的 "Elixir In Action",我发现了一些我觉得有点费解的东西。我正在研究他在第 140 页左右给出的示例,我看到了这个:
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
为什么 IO.inspect
打印值两次?是不是和第1行发送消息马上显示元组有关?
这就是 IO.inspect/2 在 iex 中的工作方式:
iex(10)> IO.inspect(3)
3 # From IO.inspect
3 # return value in iex
发生这种情况是因为 IO.inspect 打印值并且 returns 打印相同的值。 IO.puts/2 打印值并且 returns :ok
iex(11)> IO.puts(3)
3 # From IO.puts
:ok # return value in iex
我正在研究 Sasa Juric 的 "Elixir In Action",我发现了一些我觉得有点费解的东西。我正在研究他在第 140 页左右给出的示例,我看到了这个:
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
为什么 IO.inspect
打印值两次?是不是和第1行发送消息马上显示元组有关?
这就是 IO.inspect/2 在 iex 中的工作方式:
iex(10)> IO.inspect(3)
3 # From IO.inspect
3 # return value in iex
发生这种情况是因为 IO.inspect 打印值并且 returns 打印相同的值。 IO.puts/2 打印值并且 returns :ok
iex(11)> IO.puts(3)
3 # From IO.puts
:ok # return value in iex