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

SetAttributes[withTaggedMsg, HoldAll]

withTaggedMsg[exp_, label_: "When"] := Function[,
     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

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[] := 
  Internal`InheritedBlock[{MessagePacket}, Unprotect[MessagePacket];
   MessagePacket[name__, BoxData[obj_, form_]] /; ! TrueQ[$tagMsg] := 
            Block[{$tagMsg = True}, 
           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!
  • 1
    $\begingroup$ I wonder if this should be addressed directly to @F'x. $\endgroup$ – Jens Apr 3 '16 at 4:20
  • $\begingroup$ 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. $\endgroup$ – Shwouchk Apr 3 '16 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.