Annotation/Tooltip throws error on invalid Slot /Hide Slot from function

I don't know how to judge it, will think about that, meanwhile since Tooltip is meant for display anyway we can speed up formatting so that #i won't interfere with Annotiation's definitions:

shortenTrace // ClearAll
shortenTrace[l_List] := shortenTrace /@ l
shortenTrace[i_HoldForm] := Tooltip[
  Short[i, 0.1]
, ToString[#, InputForm]&[Unevaluated @@ i]
]

At the beginning I had MakeBoxes[i] but, as noticed by OP, it won't help with e.g. Dynamic[#] as it typesetts to DynamicBox[ToBoxes[#1, StandardForm]] so # can still interfere


Another approach is to patch the internal function itself, e.g.

WrappersDump`WrapperToBoxes[Annotation[expr_, data_], fmt_] := 
  TagBox[
    MakeBoxes[expr, fmt], 
    Function[BoxForm`x, Annotation[BoxForm`x, data]]
  ]
  • I cannot promise this will not introduce bugs of its own so in practice I would use Kuba's method, specifically the ToString variant.

  • Don't miss the three-parameter Annotation rule if you pursue this, for e.g. Annotation[0, Hold@Slot@x, "Tooltip"]


After thinking about it some more, I found an answer:

Annotation[0,Slot@i/.s:(Slot|SlotSequence):>Annotation[s,"Inactive"]]

or

Tooltip[0,Slot@i/.s:(Slot|SlotSequence):>Annotation[s,"Inactive"]]

This simply uses Annotation (kind of ironic) to hide Slot from Function. Due to the nature of Annotation, this is invisible when displayed. It is also straightforward to uniquely identify the "protected" Slots and unprotect them. One could even use a Unique symbol in place of "Inactive" to go a step further.