5
$\begingroup$

Is there any real difference between ToString[expr, form] and ToString[expr, FormatType→form]?

In all my examples, they seem to be the same. I would like to know why there are both possibilities of controlling the output. Does anyone know if there is a use for using both the second argument and the option at the same time?

$\endgroup$
  • $\begingroup$ Presumably the Option form is the actual implementation and the second argument version unravels to it. Consider: SetOptions[ToString, FormatType -> CForm] (note that this causes some funky output effects. Reset it by replacing CForm with OutputForm). Although the second argument does seem to take precedence over the option version. Good find. $\endgroup$ – b3m2a1 Jul 30 '17 at 4:13
  • $\begingroup$ @b3m2a1 I tried SetOptions[ToString, FormatType -> CForm], but nothing funky happened. ToString just converts everything to CForm normally (Mathematica 10.3 Mac OS X 10.10). Maybe I'm missing something? $\endgroup$ – QuantumDot Jul 30 '17 at 4:18
  • $\begingroup$ The funkiness I found was in things like the output labels and presumably that will propagate through anywhere a bare ToString is used. Just a preemptive warning to anyone trying that and later being confused as to why things are displaying differently from expected. $\endgroup$ – b3m2a1 Jul 30 '17 at 4:19
5
+100
$\begingroup$

There is no difference between using a secong argument which is a member of $PrintForms and using the option FormatType. If the second argument is used, the option value is ignored.

The reason both exist is historical. In V1 and V2, ToString had no second argument, and its options were documented to be equal to that of OpenAppend. In V3, the second argument was added. This is partly because it was by far the most important option that could be given to ToString, and also that OpenAppend started having options that really didn't make sense for ToString (e.g., BinaryFormat). While the documentation continued to state for many versions that it could take all the options of OpenAppend, all the "new" options were silently ignored.

[I busted out a copy of The Mathematica Book, 2nd Edition, just to make sure my historical reconstruction was correct. :)]

| improve this answer | |
$\endgroup$
  • $\begingroup$ That's interesting! I should go to a library and have a look at some of the earlier editions of The Mathematica Book. It might be fun to see how Mathematica has evolved over the years. $\endgroup$ – QuantumDot Jul 31 '17 at 3:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.