[GOODIE] Arithmetic assignments (+= and friends)

PhiHo Hoang phiho.hoang at rogers.com
Mon Jun 17 01:23:01 UTC 2002


Andreas,

	This is cool. I did 4 'printit'. Is the last one 'a *= b'
expected ?

a := 7 	7
b := 5 	5

a += b 	12
a *= b 	35


	Cheers,

	PhiHo.


-----Original Message-----
From: squeak-dev-admin at lists.squeakfoundation.org
[mailto:squeak-dev-admin at lists.squeakfoundation.org] On Behalf Of
Andreas Raab
Sent: Sunday, June 16, 2002 8:23 PM
To: squeak-dev at lists.squeakfoundation.org
Subject: [GOODIE] Arithmetic assignments (+= and friends)


Folks,

I finally figured out how to get arithmetic assignments (like +=, -=,
*=) right in Squeak (I always wanted this...) The attached CS changes
the compiler so that it will compile all of them correctly. Besides less
typing this is in particular useful if you have polymorphic objects
which do implement "inplace arithmetic" like float arrays and may want
to use it interchangeably with "atomic types". You can now do stuff
like:

	a := 4.
	b := 3.
	a += b.
	a => 7

as well as:

	a := 4 at 0@4.
	b := 3 at 3@0.
	a += b.
	a => a B3DVector3(7.0 3.0 4.0)

etc. If you're interested in playing with this please note that an
expression like:

	a += b

is really compiled into:

	a := a perform: #+= with: b.

e.g., the message #+= is sent before the resulting assignment. This is
so that we can both support "atomic types" (like numbers) which
implement

Number>>+= aNumber
	^self + aNumber

as well as more complex implementations which may return self after
performing some inplace operation.

Enjoy,
  - Andreas




More information about the Squeak-dev mailing list