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?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityWhen 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
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 *)