4

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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Just calling Format[_foo, _] := bar gives the message Format::fttp: Format type _ is not a symbol. >> – Greg Hurst Jan 19 '15 at 20:18
  • @ChipHurst Well crud, I copied the wrong code -- again -- fix it in a moment. Thanks. – Mr.Wizard Jan 19 '15 at 20:18
  • Well you had the extra pattern p :, so I wasn't sure if you had seen this message before. – Greg Hurst Jan 19 '15 at 20:23
  • @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. – Mr.Wizard Jan 19 '15 at 20:25

0 Answers0