[GOODIE] Arithmetic assignments (+= and friends)

Stephen Pair spair at advantive.com
Mon Jun 17 15:10:46 UTC 2002


Another possible approach to this issue (if it's an issue at all) is to
come up with some keyboard short cuts.  For example, typing "<space> +
=" would expand into " := X + " (where X is the word immediately
preceding where the shortcut was typed).  A preference would control
whether these shortcuts are enabled.

- Stephen 

> -----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