Skip to content
Kavachकवच

Standard Library: std.tasks

● Intermediate5 min

Every type, method and function of std.tasks: what the spec lists, then each declaration in its source, with its examples and the line that declares it.

Spec 9.4 · learn it in Channels.

A declaration below shows what a program writes against: a type's fields, a function's parameters, what it gives, its contracts (require, ensure) and its examples. kavach test runs every example, so each one is true of the code.

What the spec lists

MethodDoes
send.put(v)Adds v. When the channel already holds capacity values, it waits until one is taken. Putting into a closed channel panics, and so does putting into one whose receivers are all gone: nobody could ever take the value. A put waiting for room panics when the last receiver goes.
send.try_put(v) -> BoolAdds v if the channel has room and gives true; when it is full or closed, or its receivers are all gone, gives false at once and v is let go. It never waits, so one slow taker can't hold up a task that sends to many.
send.close()No more values will come. Values already in the channel can still be taken.
receive.take() -> Taken<T>The next value, in the order they were put, as Taken.Item(value). It waits while the channel is empty, and gives Taken.Closed once the channel is closed and empty.

std.tasks · tasks.kav

use std.tasks: 1 type and 0 functions. stdlib/tasks/tasks.kav

Taken type

What receive.take() gives: the next value, or Closed once the channel is closed and empty. A none sent on a Channel<T?> is Item(value: none), never Closed (decision 0097).

pub type Taken<T> = Item(value: T) | Closed