A while loop macro hygiene problem

The only thing I see is:

iex(1)> c "while.ex"
warning: variable "x" is unused
while.ex:29
[Test, My]
iex(2)>

And it turns out it has nothing to do with macros:

But…try is a macro, so I think we need to adjust the example:

defmodule Test do

  def go do
    try do
      x = 7  # Inside the try

      for _ <- Stream.cycle([:ok]) do
        if x > 4 do
          x = x - 1
          IO.puts x  # switched order
        else
          throw :break
        end
      end
    catch
      :break -> :ok
    end
  end

end

…and with the IO.puts statement after x = x - 1, I no longer get the warning. But, the result is the same–an endless loop of output, which I think proves your point about the closures. There’s no warning that x is undefined, so the value of x is retrieved from the surrounding context, and the statement x = x - 1 creates a new local x inside the for-block.

If you look at the examples in the book (p.25) you should notice that there is nothing in block that is driving towards the terminating condition for expression

Yes, that is what prompted me to try to create a more general purpose while loop. I thought the example in the book was a pretty crippled example of a while loop and didn’t live up to its hype.