[GOODIE] Arithmetic assignments (+= and friends)

Andreas Raab Andreas.Raab at gmx.de
Mon Jun 17 00:23:16 UTC 2002


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ArithmeticAssign.1.cs
Type: application/octet-stream
Size: 3938 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20020617/848da3ad/ArithmeticAssign.1.obj


More information about the Squeak-dev mailing list