[Q] Fiddling with system innards, was [Q] Newbie - Multiple Return Values

Richard A. O'Keefe ok at cs.otago.ac.nz
Fri May 2 03:02:32 UTC 2003


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.



More information about the Squeak-dev mailing list