Circling Multi-Returns: Choose Which Is Expression Result

Multi-Returns have a feature of being able to "circle" which result will be the value of the overall expression. You do the circling with an @word or an @tu.p.le... or an @(gr o up) that evaluates to what you want to assign.

Demo!

Here's a function with a secondary return value to demonstrate with:

multi: func [
    return: "Main return value"
        [integer!]
    @secondary: "Secondary return value"
        [integer!]
][
    secondary: 20
    return 10
]

Traditionally you could ask for both results, and the overall result would be the first return:

>> [a b]: multi
== 10  ; defaults to same value as first result, e.g. A

>> a
== 10

>> b
== 20

But now you can "circle" to ask for a different result, e.g. the second:

>> [a @b]: multi
== 20  ; same value as b

>> a
== 10

>> b
== 20

You can combine this with things like GROUP!s to calculate the variable name, or even omit the variable name and use #:

>> [a @(#)]: multi
== 20  

>> a
== 10

There are some places where this comes in handy, although you generally want to pick the main return to be the thing people are most interested in responding to (e.g. determining success or failure of an operation).

There's also a characteristic that only the main return can return unstable isotopes without having to contort the value convention itself to encode the void state (e.g. by ^META quoting the results). So that should be weighed as well when thinking about how to plan the results.

2 Likes