Re: Dynamic scoping
- To: mathgroup at smc.vnet.net
- Subject: [mg129700] Re: Dynamic scoping
- From: Richard Fateman <fateman at cs.berkeley.edu>
- Date: Tue, 5 Feb 2013 03:06:10 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
- References: <keiatl$qq3$1@smc.vnet.net> <kel4qe$38d$1@smc.vnet.net>
On 2/2/2013 11:49 PM, Rolf.Mertig at gmail.com wrote:
...
>
> Block[{Plus = "Plus"}, t = ToString[Plus[1, 2]];
> Print[t];]
> will do what you want
>
No, I think what he wanted was to display a bug in Mathematica.
You just wrote another program.
To illuminate the bug I tried this:
Addition[x_,y]:=x+y;
Block[{Plus=Addition},Print[Plus[1,2]]]
which returns an infinite stream of errors that look like
Join::heads: Heads PacletManager`Package`LDCfindForDocResource and
PacletManager`Package`MCfindForDocResource at positions 1 and 2 are
expected to be the same.
I think the explanation needs some explanation, but setting that aside
I tried these:
Block[{P = Addition}, Print[P[1, 2]]]
prints 3
Block[{P = foo}, Print[P[1, 2]]]
prints foo[1,2]
An explanation may be simply that
Block[{Plus}} ....]
binds the name Plus not to some "undefined object" but to the value of
Plus in the dynamically enclosing scope. Indeed, this works as
expected:
Block[{Plus = hello}, Block[{Plus = Plus}, Plus[1, 2]]]
returns hello[1,2]
this explanation is not accurate because
Block[{Plus = hello}, Block[{Plus}, Plus[1, 2]]]
returns 3.
So Block[{Plus}, ...
binds the name Plus, or any other Global symbol? to the Global value.
I think that this is a bug.
RJF