MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: StringJoin with ToString

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98315] Re: StringJoin with ToString
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Sun, 5 Apr 2009 06:40:56 -0400 (EDT)
  • References: <gr6cbs$pva$1@smc.vnet.net>

replicatorzed at gmail.com wrote:
> It is quite interesting why StringJoin does not apply ToString to
> those arguments which are non-string. I've overridden the built-in
> StringJoin, to save a few unnecessary ToStrings by the standard method
> (the context specifications are necessary for packaging):
> 
> Unprotect[System`StringJoin];
> $UseNewStringJoin = True;
> System`StringJoin[expr___] :=
>   Block[{$UseNewStringJoin = False},
>     StringJoin@(ToString /@ {expr})] /; TrueQ[$UseNewStringJoin];
> Protect[$UseNewStringJoin];
> Protect[System`StringJoin];
> 
> This seems to work (after loading the package) for any cases, e.g.:
> 
> In[2]:= StringJoin[1, 2, 3]
> 
> Out[2]= "123"
> 
> In[3]:= 4 <> 5 <> 6
> 
> Out[3]= "456"
> 
> but has a strange effect on messages:
> 
> In[5]:= Append[dummy, 2]
> 
> During evaluation of In[5]:= Append::normal: {Nonatomic expression
> expected at position , {1},  in , {Append[dummy,2]}, .} >>
> 
> Out[5]= Append[dummy, 2]
> 
> that is: the message strings are displayed in a non-joined list
> format.
> The code below shows an other modified StringJoin, that gives the
> exact same results (after initiating a fresh kernel):
> 
> Unprotect[System`StringJoin];
>        System`StringJoin[expr___]:=Module[{str=""},
>        Scan[(Print[#]; str=StringInsert[str,ToString[#],-1])&,{expr}];
>        str
> ];
> Protect[System`StringJoin];
> 
> Now I've inserted a Print[#]; inside the function to see what's
> happening. This reveals some internal calculation that is done during
> the evaluation of the erroneous call of Append[dummy, 2], which I
> don't really understand (did not copy here for size constraints).
> Apart from this strange sideeffect on messages (which seems to be
> harmless), both functions work as expected. Does anyone have any idea
> what is the exact cause of this behaviour and how to overcome it?
> 
> Thanks
> 
> Istvan Zachar
> 
> 
> 
I would never routinely override built-in commands - even to extend 
their functionality. There are lots of reasons for this, but not least 
is the fact that code that uses this trick is very hard to read. Why not 
simply write a function ExtendedStringJoin.

Part of Mathematica is written using other Mathematica functions - so 
problems such as you report are not unexpected.

On the face of it, adding extra functionality to a built-in might not be 
expected to cause problems, but it is easy to write code that would be 
disrupted by your change:

ss = Quiet[2 <> 3];
Map[ToString[#^2] &, ss]

Who knows what sort of code lies inside Mathematica :)

David Bailey
http://www.dbaileyconsultancy.co.uk


  • Prev by Date: Re: Dynamic 2D ListLinePlot PlotRange
  • Next by Date: Re: Problem with Import[] and math.exe
  • Previous by thread: StringJoin with ToString
  • Next by thread: Re: StringJoin with ToString