Consider this example: given an option for a function func as "x" :> (a&), how can one assign different values to a locally inside func?
a = False; (* global value of a *)
func[opt_] := With[{a = True}, Evaluate["x" /. opt]]; (* oversimplified function *)
func["x" :> (a &)] (* ==> a & *)
No amount of Evaluate seems to help here. The problem with With is that in this case it is interpreted as With[{a$ = 1}, a&], thus no replacement will be done. If I use Block instead of With the problem boils down to this (as Block won't replace variables inside Function):
Block[{a = True}, a &] (* ==> a & *)
I do not want to define the option to be a function depending explicitly on a: I expect the user to write his own pure function for "x" where he can also use a that should acquire its local value when the With/Block is evaluated. Also, the problem disappears, if the code is simplified, but of course this is a no-go, as I need a function:
opt = "x" :> (a &);
With[{a = True}, Evaluate["x" /. opt]] (* ==> True & *)
I've checked the following posts (mostly by Leonid and Mr.W), none of which offered a clear solution: this, this and this.
f[opt_] := "x" /. (opt /. HoldPattern[a] -> True)? – ssch Oct 19 '13 at 11:36func[opt_] := Block[{a = True}, "x" /. opt /. HoldPattern[a] -> a]– Michael E2 Oct 19 '13 at 12:09Blockworth a comment, since Istvan mentionedataking on local values. I saw you had answered, and saw it was general, but it took a while for me to figure out what it was doing. I wish I was as fluent as you (as you seem to be) in thinking about these problems, which is why I try to solve them. (Upvoted your answer, btw.) – Michael E2 Oct 19 '13 at 12:33