5
$\begingroup$

Is it possible to localize an assignment to an expression (defined via UpSet) instead of a Symbol within a scoping construct?

It would be nice if I had something like the following at my disposal:

Block[{f[1,2]=ab}, (*<-- what I need*)
  Table[f[i,j],{i,1,2},{j,1,2}]
]

(I need to use Block. Module and With are not correct for my case.)

$\endgroup$

1 Answer 1

11
$\begingroup$

If you need to preserve the definitions of a Symbol with exception(s) to some particular case(s) you should use Internal`InheritedBlock:

f[a_, b_] := a + b;

Internal`InheritedBlock[{f}, f[1, 2] := ab;
 Table[f[i, j], {i, 1, 2}, {j, 1, 2}]]

f[1, 2]
{{2, ab}, {3, 4}}

3
$\endgroup$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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