I have come across the (internal) use of the function:
Internal`LocalizedBlock
I am trying to determine its purpose. It seem to behave like Internal`InheritedBlock
except that a starting value (e.g. {x = 3}
) cannot be set.
x = "global"; f[] := x
Internal`LocalizedBlock[{x}, {x, x = 7, f[], Hold[x]}]
x
Internal`InheritedBlock[{x}, {x, x = 7, f[], Hold[x]}]
x
{"global", 7, 7, Hold[x]}
"global"
{"global", 7, 7, Hold[x]}
"global"
What purpose does this function serve? Why would it be used in place of InheritedBlock
?
Answer
Internal`LocalizedBlock
behaves the same as Block
, but it can localize non-Symbols (e.g. f[1]
, Subscript[x, 0]
, etc.).
For example,
Internal`LocalizedBlock[{Subscript[x, 0]}, Subscript[x, 0] = 1]
(* 1 *)
Compare this to
Block[{Subscript[x, 0]}, Subscript[x, 0] = 1]
(* During evaluation of In[79]:= Block::lvsym: Local variable specification {Subscript[x, 0]} contains Subscript[x, 0], which is not a symbol or an assignment to a symbol. >> *)
(* Block[{Subscript[x, 0]}, Subscript[x, 0] = 1] *)
It's also worth noting that one cannot assign values in the first argument of Internal`LocalizedBlock
Comments
Post a Comment