Skip to main content

function construction - How do I localize an assignment to expression (instead of symbol) within scoping construct?



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.)



Answer



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


Comments