When evaluatingf[x_:x0] := x, x0 will be evaluated, regardless of the HoldAll attribute of SetDelayed.
Why the second argument of Optional is evaluated in a non-standard manner?
When evaluatingf[x_:x0] := x, x0 will be evaluated, regardless of the HoldAll attribute of SetDelayed.
Why the second argument of Optional is evaluated in a non-standard manner?
From the "Details" of the docs for HoldPattern
The left-hand sides of rules are usually evaluated, as are parts of the left-hand sides of assignments. You can use
HoldPatternto stop any part from being evaluated.
Application:
ClearAll[f];
f[HoldPattern[x_: x0]] := x;
x0 = 7;
f[5]
f[]
(*
5
7
*)
Block[{x0 = 3},
f[]
]
(* 3 *)