17
$\begingroup$

Bug introduced in 8.0 or earlier and fixed in 10.1


When assigning message to a symbol localized by Block, I would expect that this message is not accessible outside of Block, but I observe different behavior:

ClearAll[x]
Block[{x}, x::test1 = "message1"];
x::test1
(* "message1" *)

Message was assigned to x globally regardless of x being localized.

It also happens with direct assignment to Messages:

ClearAll[x]
Block[{x}, Messages[x] = {HoldPattern[x::test2] :> "message2"}];
x::test2
(* "message2" *)

I get this behavior in all versions I have available: 8.0.0, 9.0.1 and 10.0.0 on Linux.

Is this a bug?

$\endgroup$
5
  • 1
    $\begingroup$ I think this happens because MessageName has certain auto-stringification step, which allows the second argument of MessageName to be a symbol. When the definition is constructed, it is auto-stringified, but then still attached to the symbol. From the point of view of what Block is supposed to do, this can be probably considered a bug. $\endgroup$ Feb 7, 2015 at 13:21
  • $\begingroup$ @LeonidShifrin But it also happens when directly assigning to Messages list. Is MessageName involved also in this kind of assignment? $\endgroup$
    – jkuczm
    Feb 7, 2015 at 13:24
  • $\begingroup$ I think it has to. May be not MessageName specifically, but I think that the underlying mechanism used to attach message(s) to symbol(s) should be the same in both cases. And it is this mechanism whose interaction with Block looks problematic. $\endgroup$ Feb 7, 2015 at 14:06
  • $\begingroup$ @Leonid Are you comfortable adding the bugs tag to this one? $\endgroup$
    – Mr.Wizard
    Feb 8, 2015 at 15:23
  • $\begingroup$ @Mr.Wizard I think so. I just did. $\endgroup$ Feb 8, 2015 at 15:29

2 Answers 2

8
$\begingroup$

This has been fixed in 10.1 (windows)

Mathematica graphics


Mathematica graphics

code

ClearAll[x]
Block[{x}, x::test1 = "message1"];
x::test1

ClearAll[x]
Block[{x}, Messages[x] = {HoldPattern[x::test2] :> "message2"}];
x::test2

Block[{x}, x /: x::foo = "bar"; Message[x::foo]];
Messages[x]
$\endgroup$
8
$\begingroup$

My initial answer was simply wrong. I now think this is a bug. Although a message may be expressly attached to the Symbol x using TagSet it is still not localized. (TagSet is superfluous but it helps make the point.)

Block[{x}, x /: x::foo = "bar"; Message[x::foo]];

Messages[x]

During evaluation of In[]:= x::foo: bar

{HoldPattern[x::foo] :> "bar"}

In fact attempting to attach the rule to MessageName yields this:

MessageName /: x::foo = "bar";

MessageName::tag: Rule for MessageName of x::foo can only be attached to x. >>

$\endgroup$
4
  • $\begingroup$ But Subscript has no "automagic" defined like MessageName. Assigning to MessageName really assigns to symbols Messages and it also happens when assigning directly to Messages which I expected to behave like DownValues etc. $\endgroup$
    – jkuczm
    Feb 7, 2015 at 11:15
  • $\begingroup$ @jkuczm Nevertheless the underlying cause may be the same, in that the definition is not attached to the Symbol x. I am working on this now. Sorry for the premature answer. $\endgroup$
    – Mr.Wizard
    Feb 7, 2015 at 11:16
  • 2
    $\begingroup$ I thought it should assign to x. Language`ExtendedDefinition[x] gives among others Messages list with message assigned inside Block. $\endgroup$
    – jkuczm
    Feb 7, 2015 at 11:18
  • $\begingroup$ @jkuczm Good point. Answer rewritten. $\endgroup$
    – Mr.Wizard
    Feb 7, 2015 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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