[GOODIE] Arithmetic assignments (+= and friends)
Stephen Pair
spair at advantive.com
Mon Jun 17 14:59:34 UTC 2002
Andreas,
You could also add special unary types to the compiler in a similar way
as the BinaryTypes. This could allow the postfix ++ message (prefix
would be a little harder). The increment would happen after the
expression where the ++ message was found. Then you could write:
C := 1.
Smalltalk lookLike: C++.
This would assing 1 to C, then send #lookLike: with an argument of 1 to
Smalltalk, followed by incrementing the C global by 1 and assigning it
back to the global after sending #lookLike:.
- 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
|