9
$\begingroup$

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?

$\endgroup$
2

1 Answer 1

9
$\begingroup$

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 HoldPattern to 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  *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.