4
$\begingroup$

I noted while writing this answer that using an external Pattern on Format doesn't work:

p : Format[_foo] := bar

foo[1]
foo[1]  (* did not work *)

The FormatValues reflect the definition:

FormatValues[foo]
{HoldPattern[p : _foo] :> bar}

However the definition is not in the correct form. Compare the one without Pattern:

ClearAll[foo];  Format[_foo] := bar;  FormatValues[foo]
{HoldPattern[MakeBoxes[_foo, FormatType_]] :> Format[bar, FormatType], 
 HoldPattern[_foo] :> bar}

Note that here the rule is rewritten in terms of MakeBoxes

foo[1]
bar  (* it works now *)

Is this behavior a bug or is there a reason for Pattern to prevent the standard rule formation?

$\endgroup$
  • $\begingroup$ Just calling Format[_foo, _] := bar gives the message Format::fttp: Format type _ is not a symbol. >> $\endgroup$ – Chip Hurst Jan 19 '15 at 20:18
  • $\begingroup$ @ChipHurst Well crud, I copied the wrong code -- again -- fix it in a moment. Thanks. $\endgroup$ – Mr.Wizard Jan 19 '15 at 20:18
  • $\begingroup$ Well you had the extra pattern p : , so I wasn't sure if you had seen this message before. $\endgroup$ – Chip Hurst Jan 19 '15 at 20:23
  • $\begingroup$ @Chip I was experimenting, trying to better understand this before I posted the question, and I ended up copying the wrong section. I think now I have the question as intended. $\endgroup$ – Mr.Wizard Jan 19 '15 at 20:25

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.