Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

What does the function Internal`InheritedBlock do? How is it different from the regular Block?

share|improve this question
1  
Popular use of this function started with Daniel's MathGroup post: forums.wolfram.com/mathgroup/archive/2010/Dec/msg00710.html –  Mr.Wizard May 25 '13 at 15:21
add comment

1 Answer 1

up vote 30 down vote accepted

Internal`InheritedBlock (IIB) is similar to Block, except that it preserves the original definition of the function being passed to it. The function can then be modified as we wish inside the IIB without affecting the external definition.

Let's see how Block works first:

f[x_] := x
Block[{f},
    Print@DownValues[f];
    f[x_, y_] := x y;
    Print@DownValues[f];
];
Print@DownValues[f];

(* {} *)
(* {HoldPattern[f[x_, y_]] :> x y} *)
(* {HoldPattern[f[x_]] :> x} *)

You notice that the original definition of f is not available inside the Block. On the other hand, look at what IIB does:

f[x_] := x
Internal`InheritedBlock[{f}, 
    Print@DownValues[f];
    f[x_, y_] := x y;
    Print@DownValues[f];
];
Print@DownValues[f];

(* {HoldPattern[f[x_]] :> x} *)
(* {HoldPattern[f[x_]] :> x, HoldPattern[f[x_,y_]] :> x y} *)
(* {HoldPattern[f[x_]] :> x} *)

The original definition of f is available inside its scope, and new definitions are added to the existing ones (as is the usual behaviour), unless you explicitly clear them.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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