If I execute a line of code with Evaluate
I find some unexpected behavior when the code returns an error. Here is a minimal example. Let f
f[x_, y_] := (WriteString["stdout", "Hi "];
y /. x);
Now if I call
In[1]:= Evaluate[f[a->1,a]]
Hi
Out[1]= 1
As expected, while if I do
In[2]:= Evaluate[f[a,b]]
this obviously results in an error (because a
is not a Rule
), but I can see that the function is evaluated multiple times
Hi Hi Hi
ReplaceAll: {a} is [etc...]
Out[2]= b/. a
As you see it is evaluated three times. The same instruction without the Evaluate
returns the same but with only one "Hi."
I suspect that in order to throw the error Mathematica is enclosing that statement inside a held function and the Evaluate is causing it to evaluate again. But I can't figure out what's happening in detail.