MathGroup Archive 2011

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

Search the Archive

Re: Bad design in Set?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118621] Re: Bad design in Set?
  • From: Todd Gayley <tgayley at wolfram.com>
  • Date: Wed, 4 May 2011 19:47:24 -0400 (EDT)

>On Mon, 02 May 2011 05:50:36 -0500, Alexey <lehin.p at gmail.com> wrote:
>
> > Recently I discovered that in Mathematica 7.01 the built-in function
> > Set is overloaded by default in a **very** strange way:
> >
> >     In[1]:= DownValues[Set]
> >
> >     Out[1]= {HoldPattern[
> >        JLink`Private`sym_Symbol[JLink`Private`arg_Symbol] =
> >         JLink`Private`val_] :>
> >       With[{JLink`Private`obj = JLink`Private`sym},
> >        JLink`CallJava`Private`setField[
> >          JLink`Private`obj[JLink`Private`arg], JLink`Private`val] /;
> >         Head[JLink`Private`obj] === Symbol &&
> >          StringMatchQ[Context[JLink`Private`obj], "JLink`Objects`*"]]}
> >
> > The l.h.s of this definition matches any expression of the form
> > 'a[b]=c' that is a very general and commonly-used form of assignment.
> > Moreover, in the r.h.s of this definition we see an expression of the
> > form
> >
> > JLink`CallJava`Private`setField[_]/;condition
> >
> > It is easy to see that the call to Java will be made regardless of the
> > 'condition', IOW **always** when the l.h.s matches:
> >
> > Print["!"]/;False
> >
> > prints "!".
> >
> > Is it reasonable to make a call to Java every time when a user makes
> > an assignment of the form 'a[b]=c'?


At the start, let me point out that in Mathematica 8, J/Link no 
longer overloads Set. An internal kernel mechanism was created that, 
among other things, allows J/Link to avoid the need for special, er, 
"tricks" with Set.

J/Link has overloaded Set from the very beginning, almost twelve 
years ago. This allows it support this syntax for assigning a value 
to a Java field:

     javaObject@field = value

The overloaded definition of Set causes a slowdown in assignments of the form

     _Symbol[_Symbol] = value

Of course, assignment is a fast operation, so the slowdown is small 
in real terms. Only highly specialized types of programs are likely 
to be significantly affected.

The Set overload does _not_ cause a call to Java on assignments that 
do not involve Java objects (this would be very costly). This can be 
verified with a simple use of TracePrint on your a[b]=c.

It does, as you note, make a slight change in the behavior of 
assignments that match _Symbol[_Symbol] = value. Specifically, in 
f[_Symbol] = value, f gets evaluated twice. This can cause problems 
for code with the following (highly unusual) form:

     f := SomeProgramWithSideEffects[]
     f[x] = 42

I cannot recall ever seeing "real" code like this, or seeing a 
problem reported by a user.

This is all moot now in 8.0.


At 04:45 AM 5/3/2011, DrMajorBob wrote:
>Calls to Java are made for ABSOLUTELY everything Mathematica does, are
>they not?

Actually, calls to Java are made for a comparatively small set of 
things in Mathematica. The vast majority of Mathematica programs will 
never trigger a single call into Java.


Todd Gayley
Wolfram Research






  • Prev by Date: NMaximize inconsistency
  • Next by Date: Re: Replacements and NIntegrate
  • Previous by thread: Re: Bad design in Set?
  • Next by thread: Re: Limit[f[x], x->a] vs. f[a]. When are they equal?