Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?

10

1

Consider the following expression:

Block[{Print = CopyToClipboard}, doSomething[]]

Now all calls to Print from doSomething[] (and all other functions it may invoke) will copy values to clipboard rather than printing them. Suppose I want to modify this expression such that all calls to Print both copy values to clipboard and print them.

Block[{Print = (CopyToClipboard[#]; Print[#]) &}, doSomething[]]

I need something similar to the above, but which doesn't go into an infinite recursion and where the occurrence of Print within the pure function retains its global meaning that it had before the execution entered the Block. Is it possible?

I understand the difference between Block and Module, and I need to use Block to create a dynamic (rather than lexical) scoping.

Vladimir Reshetnikov

Posted 2013-06-17T23:41:43.620

Reputation: 6 772

3

Since you are augmenting a behavior, you are looking for the Villegas-Gayley trick. There are many examples on this site, my personal favorite is from Mr.Wizard.

– rcollyer – 2013-06-18T01:06:55.683

@rcollyer thanks for the shout-out! :-) – Mr.Wizard – 2013-06-18T15:30:52.900

@Mr.Wizard you're welcome. I'm tempted, if I find the time, to add to the design patterns question the most fundamental pattern in mma: there is no distinction between code and data, and that is a perfect example of it. – rcollyer – 2013-06-18T15:32:16.840

2

@rcollyer Probably you know it, but I'd like to mention that languages with this property are called homoiconic.

– Vladimir Reshetnikov – 2013-06-18T20:34:01.343

@VladimirReshetnikov yes, having recently learned that word ... and it has some very interesting consequences. – rcollyer – 2013-06-18T20:37:55.337

Answers

9

What you are looking for is the Villegas - Gayley technique:

Internal`InheritedBlock[{Print},
    Unprotect[Print];
    Module[{inPrint},
       Print[arg_]/;!TrueQ[inPrint]:=
          Block[{inPrint = True},
              (CopyToClipboard[#]; Print[#])&[arg]
          ];
    ];
    your-code
]

Here, the Internal`InheritedBlock is used to make sure that the redefinition of Block remains local to your execution stack - it is also a dynamic scoping construct, but it preserves the old definitions, unlike Block.

Leonid Shifrin

Posted 2013-06-17T23:41:43.620

Reputation: 108 027

1

StackOverflow reference: http://stackoverflow.com/a/5149656/618728

– Mr.Wizard – 2013-06-18T15:34:25.857