On this page:
values
values
values
values
call_  with_  values
8.12

6.47 Multiple Values🔗ℹ

function

fun values(v :: Any, ...)

Returns the vs as multiple result values.

If only one v is provided, the result is the same as just v. Any other number of values must be received by a context that is expecting multiple values, such as with a values binding pattern.

binding operator

values(bind, ...)

The values binding operator can only be used in places where it’s specifically recognized, normally to match multiple result values. For example, the lhs_bind position of def recognizes values.

> def values(x, y) = values(1, 2)

> x+y

3

annotation

values(annot, ...)

The values annotation can only be used in places where it’s specifically recognized, normally to annotate multiple result values. For example, the maybe_res_annot position of fun recognizes values.

> fun two_numbers() :: values(Int, Int):

    values(1, 2)

> def (x, y) = two_numbers()

> x+y

3

reducer

values(id maybe_annot init, ...)

 

maybe_annot

 = 

:: annot

 | 

:~ annot

 | 

ϵ

 

init

 = 

= expr

 | 

: body; ...

A reducer used with for, expects as many results from a for body as ids. When id is _, a fresh identifier is used, otherwise id is bound as follows. For the first iteration of the for body, each id’s value is the result of the corresponding init. The results of a for body for one iteration then serve as the values of the ids for the next iteration. The values of the whole for expression are the final values of the ids.

function

fun call_with_values(producer :: Function.of_arity(0),

                     consumer :: Function)

Calls producer with no arguments, and then calls consumer with the result value(s) from producer.

Use call_with_values to dispatch on the number of values that are produced by an expression. The match form cannot make that distinction, because it always expects a single result value from its initial subexpression.

fun get_fruit(n :: NonnegInt):

  match n

  | 0: values()

  | 1: "apple"

  | 2: values("apple", "banana")

  | ~else: values("apple", n +& " bananas")

fun

| show(): println("nothing")

| show(s): println(s)

| show(a, b): println(a +& " and " +& b)

> call_with_values(fun (): get_fruit(0), show)

nothing

> call_with_values(fun (): get_fruit(1), show)

apple

> call_with_values(fun (): get_fruit(2), show)

apple and banana

> call_with_values(fun (): get_fruit(3), show)

apple and 3 bananas