defmodule R do
def e(retry_num) do
resp = {:error, "adsfs"}
if retry_num==4 do
resp
else
case resp do
{:error, _body} ->
e(retry_num + 1)
any -> any
end
end
end
end
in this module if statement prints resp variable to stdout but below one doesn’t
defmodule R do
def e do
resp = {:error, "dsd"}
if resp == resp do
resp
else
IO.puts "false!"
end
IO.puts("sd")
end
end
Please format your code so its readable by inserting
```elixir
at the beginning and triple backticks at the end of the code blocks.
Secondly, what is the issue you are trying to solve? For what input are you expecting what output? Its not at all clear since you haven’t asked any question nor defined what you are trying to do. In your second module there isn’t even a variable called resp for example.
Lastly, you say “prints resp variable” but the first module has no IO statement so in fact nothing is printed. The only output is when iexinspects the final return from the function.
In the first function, any parameter to the function e/1 that is 4 or greater will have infinite recursion because you are constantly adding 1 and checking for the value 4.
I would recommend looking at a package that does this work, for example retry. Retrying external API calls has a lot of edge cases, many potential error states and different strategies for retrying you might want. Not something you need to “roll your own”.
And, BTW, this is exactly why IO.inspect returns its argument–so you can use it at the end of a function to inspect return value, or insert it into the middle of a pipeline to see what’s going on.