There are at least three ways to return multiple values from a method, and one of them is not obvious.
(1) Return an Array.
SomeClass>>multivalueMethod ... ^{x. y. z}
This made a lot of sense when Squeak let you do {a. b. c} := foo multivalueMethod.
(2) Return an object.
SomeClass>>multivalueMethod ... ^MvResult with: x with: y with: z
This is accessed like r := foo multivalueMethod. a := r x. b := r y. c := r z. Plus side: the caller has to retrieve only what the caller is interested in. Minus side: if you add more information to the result, the caller may never find out that it needs updating.
This is usually the best thing to do IF you can come up with some other things the new class can do. Can it be returned from another method? Is there result processing code that could be moved into it?
(3) Do the Scheme thing, use a block.
SomeClass>>multivalueMethod: resultBlock ... ^resultBlock value: x value: y value: z
This is accessed like foo multivalueMethod: [:x :y :z | a := x. b := y. c := z].
The thing I like about this approach is that while there still _is_ an object being created (the new Block) it doesn't _feel_ like an object. I'm not _doing_ anything to pick the result apart, I'm _given_ the result pieces.
On Fri, 2 May 2003 15:02:32 +1200 (NZST), "Richard A. O'Keefe" ok@cs.otago.ac.nz wrote:
(3) Do the Scheme thing, use a block.
SomeClass>>multivalueMethod: resultBlock ... ^resultBlock value: x value: y value: z This is accessed like
foo multivalueMethod: [:x :y :z | a := x. b := y. c := z].
The thing I like about this approach is that while there still _is_ an object being created (the new Block) it doesn't _feel_ like an object. I'm not _doing_ anything to pick the result apart, I'm _given_ the result pieces.
The other nice thing about this approach, is that the result pieces are *named*, which is why I don't like using Arrays for that purpose.
Later, Jon
-------------------------------------------------------------- Jon Hylands Jon@huv.com http://www.huv.com/jon
Project: Micro Seeker (Micro Autonomous Underwater Vehicle) http://www.huv.com
squeak-dev@lists.squeakfoundation.org