[Cuis-dev] Methods that return multiple values

Juan Vuletich juan at cuis.st
Wed Jun 21 07:08:36 PDT 2023


Hi Luciano,

The style I learn from Squeak long time ago is to receive a block and 
evaluate it with the values that would be answered. In your example it 
would be

a xgcd: b into: [ :g :s :t | ... ]

It is very similar to what you propose, but it avoids creating an extra 
array. See, for example, senders of 
#barycentricCoordinatesInTriangle:with:with:into:ifDegenerate: and 
#nextUtf8BytesAndCodePointInto:into:

This style doesn't require any additions to the image and is expressive 
enough in my opinion.

Cheers,

On 6/21/2023 10:13 AM, Luciano Notarfrancesco via Cuis-dev wrote:
> Smalltalk doesn’t have a convention for methods returning multiple 
> values, and I’m not aware of any implementation.
> An example of such thing is the extended gcd: ‘a xgcd: b’ returns g, 
> s, t where g is the gcd, and as + bt = g. Writing methods that return 
> multiple values is easy with the curly brackets syntax, 
> Integer>>#xgcd: ends with something like
>     ^ {g. s. t}
> But using sending messages that return multiple values is kind of 
> annoying, I end up doing something like:
>     xgcd := a xgcd: b.
>     g := xgcd at: 1.
>     s := xgcd at: 2.
>     t := xgcd at: 3
> Some years ago I thought about using blocks for this, but I never 
> tried it. Today I just did a little experiment implementing anArray | 
> aBlock as ‘^ aBlock valueWithPossibleArgs: self’ and I can do:
>     (a xgcd: b) | [:g :s :t| … ]
>
> This is seems quite nice already, I guess I’ll start using it and see 
> how it feels. But the point of this mail is not to show a solution, 
> but to ask if anyone have thought about this or if they know any nicer 
> solutions. Any ideas?


-- 
Juan Vuletich
cuis.st
github.com/jvuletich
researchgate.net/profile/Juan-Vuletich
independent.academia.edu/JuanVuletich
patents.justia.com/inventor/juan-manuel-vuletich
linkedin.com/in/juan-vuletich-75611b3
twitter.com/JuanVuletich



More information about the Cuis-dev mailing list