2

In the question here, Mr.Wizard gives the following code:

ClearAll[withTaggedMsg]
SetAttributes[withTaggedMsg, HoldAll]

withTaggedMsg[exp_, label_: "When"] := Function[,
   Internal`InheritedBlock[{MessagePacket},
     Unprotect @ MessagePacket;
     mp : MessagePacket[__, _BoxData] /; !TrueQ[$tagMsg] :=
           Block[{$tagMsg = True},
         Style[Row[{label, HoldForm[exp], "=", exp, " "}, "  "], "SBO"] /. tag_ :>
           MapAt[RowBox[{ToBoxes @ tag, #}] &, mp, {-1, 1}] // Identity
       ];
     #
   ],
  HoldAll]

as a way to add more information to messages generated during a long output. Here F'x extends this answer to generate the stack at the point of message creation:

SetAttributes[withTaggedMsg, HoldAll]
withTaggedMsg[] := 
 Function[, 
  Internal`InheritedBlock[{MessagePacket}, Unprotect[MessagePacket];
   MessagePacket[name__, BoxData[obj_, form_]] /; ! TrueQ[$tagMsg] := 
            Block[{$tagMsg = True}, 
     Identity@
      MessagePacket[name, 
       BoxData[RowBox[{ToBoxes@
           Style[Row[{"With stack", Stack[][[3 ;;]], Spacer[5]}, " "],
             Black], obj}], form]]];
   #], HoldAll]

My question is, can you please deconstruct what is going on here (subexpression by subexpression)? Please also pay extra attention to what Mathematica idiosyncrasies are worked around in those answers? And more specifically,

  • Why the identity map?
  • Why the need for a self referential rule? Thanks!
Shwouchk
  • 497
  • 3
  • 8
  • 1
    I wonder if this should be addressed directly to @F'x. – Jens Apr 03 '16 at 04:20
  • I figured that this is a question for which answers could benefit more than just myself, and which was answerable by more than just Mr.Wizard or F'x. – Shwouchk Apr 03 '16 at 09:06

0 Answers0