Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Odd edge-case behavior of Block

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129955] Odd edge-case behavior of Block
  • From: Roger Wilson <rogerhw999 at gmail.com>
  • Date: Wed, 27 Feb 2013 23:45:14 -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

Block is used to localize the variable cache as used in the function g when called from the function f.  In f the cache symbol to use is passed in as the second parameter z.
f[a_, z_] := Block[{cache = z}, g[a]];
g[a_] := (Print["hash" -> Hash[cache]]; cache[1] = a; cache[1]);

Everything works as expected...
Hash[z]
2065959314 (your number may be different)

Calling f...
f[1, z]
hash->2065959314 (same)
1

The symbol z now has a single downvalue...
DownValues[z]
{HoldPattern[z[1]] :> 1}

However what if I actually want to use the symbol cache as the cache symbol...

Hash[cache]
1028301578 (your number may be different)

Calling f...
f[1, cache]
hash->1028301578 (same)
1

Nothing stored in the cache this time?
DownValues[cache]
{}

My theory is that Block[{cache = cache},... effectively localizes cache to a local temporary variable but that doesn't explain why I see the right Hash for the symbol inside g.



  • Prev by Date: "Error" definition for nonlinear fit deviating from data points
  • Next by Date: Primes e Regex
  • Previous by thread: "Error" definition for nonlinear fit deviating from data points
  • Next by thread: Re: Odd edge-case behavior of Block