[GOODIE] Arithmetic assignments (+= and friends)

Lex Spoon lex at cc.gatech.edu
Tue Jun 18 14:19:05 UTC 2002


"Andreas Raab" <Andreas.Raab at gmx.de> wrote:
> Lex,
> 
> > You haven't tried to teach people to program, have you? 
> 
> I have. It's a while back and was mostly Smalltalk, but yes, I have.
> 

Whoops, I knew that.  My apologies.


> > I have the biggest success in teaching people by teaching them
> > Common Lisp, because there was little discussion at all on syntax. 
> > I've never tried Scheme, but I imagine it would be simpler.  The
> > times I've tutored or taught people in Pascal, C, or Ada, the
> > syntax has really gotten in the way. Smalltalk is hard for a
> > different reason: you have to grok a bunch of the library
> > before you can do anything. 
> 
> I agree - but the other languages are not hard simply because of += and
> friends. While I agree that "too much syntax" is hard to grasp, I would
> argue that "too little syntax" makes the experience less comfortable in
> the mid to long term.


Yes.  It's a fine line.  "x += exp" versus "x = x + exp" doesn't seem
like a big gain to me, because they are both very easy to read already. 
Since Smalltalk is already so minimal it is nice to keep it that way.

(And besides, most readable of all is to use #collect: and then #sum,
which will replace a lot of += accumulations!)


More importantly, though:


> > To return to the point, the new += seems more like := than 
> > like #value:.
> 
> Whoops? What makes you think that?! It's exactly the opposite - the
> entire intent is to ask the receiver to increase itself, e.g.,
> accurately represented it should be "foo addValue: bar". And the only
> reason the assignment form is required because some of the "more stupid"
> but unfortunately fundamental literals can't do it any other way.
> 

Is this really the desire?  Consider this sequence:

	x := 1/3.
	y := x.
	x *= 2.

Should the last statement really affect the value of y ?!  For a
value: -ish message, it would, but that would make it very different from
the original "x := x * 2".  It would also make it different from the C "*="
construct.  But indeed, it would match what I'd expect *= to mean in
Smalltalk.




> > In fact, there are existing implementations of += that
> > are more like #value: -- just check out implementors-of += 
> > to see 4 interesting implementations. 
> 
> Well, yeah, look at the initials of those methods ;-)

I know!  That makes it all the more interesting of an example.




Lex



More information about the Squeak-dev mailing list