channel
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
English reader names for the conversation family: channel, frame, send<T>, recv<T>
Syntax: let bool clausa ← s.close()
Category#
endpoint
Related#
Examples#
radix/corpus/reference/method/sermo/sermo-close.fab (canonical · method)#
A channel closes itself when its conversation completes; a pool of many keeps only the unfinished ones open, and close() ends one early and reports its error.
# =============================================================================
# channel — a conversation closes itself when it completes; `close()` is early.
# =============================================================================
#
# What this teaches:
# • Completion closes — when the handler returns (or fails), the channel
# releases its handler task and router slot at once, even while its
# handle still sits in a map; the handle keeps reading as its result
# • A pool of many — finished conversations cost nothing while the pool
# waits on the rest, so a map of many sermones is cheap
# • Explicit close — `s.close()` ends a conversation early (a live handler
# is cancelled, unread frames are dropped) and reports the error the
# conversation ended with through `⇥ string`; it returns `true` when
# this call closed the handle
# • Last reference — a channel nobody can reach is closed as a safety net,
# but errors from that automatic close are lost; call `close()` when the
# error matters
#
# Common mistakes:
# • expecting close() to wait — a handler still running is cancelled, so
# read to the end first when its outcome matters
# • keeping a channel inside a reference cycle — a cycle never reaches its
# last reference, so the compiler warns (WARN031) and it never
# auto-closes; `close()` it explicitly
#
# See also: call, channel, recv, do, catch
# =============================================================================
@ call 'numeri:bis'
fn bis(int n) → int {
return n * 2
}
@ call 'numeri:numera'
fn numera(int n) generator → int {
for range 0 ‥ n let i {
yield i
}
}
@ call 'numeri:defice'
fn defice(int n) → int ⇥ string {
throw "defecit §"(n)
}
fn sum(list<int> partes) → int {
mut int totum ← 0
for from partes let pars {
totum ← totum + pars
}
return totum
}
main {
# Four conversations finish at once; their handles wait in the pool.
mut map<int, channel> piscina ← empty
for range 0 ‥ 4 let i {
piscina[i] ← call 'numeri:bis' (i)
}
# A longer conversation streams while the finished ones hold nothing.
let channel longus ← call 'numeri:numera' (5)
for range 0 ‥ 4 let i {
print piscina[i] ↦ int
}
print sum(longus ↦ list<int>)
# Reading waits for the handler (a failed read defaults to 0); close()
# then reports the error the conversation ended with.
let channel fractus ← call 'numeri:defice' (7)
print fractus ↦ int ⊥ 0
do {
let bool clausa ← fractus.close()
print clausa
}
catch err {
print err
}
# Closing early drops the unread reply; a live handler is cancelled.
do {
let channel ultimus ← call 'numeri:bis' (21)
let bool clausa ← ultimus.close()
print clausa
}
catch err {
print err
}
}Expected output:
0
2
4
6
10
0
defecit 7
verum
radix/corpus/reference/type/sermo/sermo-channel.fab (canonical · type)#
English reader names for the conversation family: channel, frame, send<T>, recv<T>
# =============================================================================
# channel — the conversation family under the English reader surface.
# =============================================================================
#
# What this teaches:
# • `channel` is the en spelling of `channel`, the handle `call` returns
# • `frame<T>` is the en spelling of `frame<T>`, one typed frame
# • `s.send<T>()` / `s.recv<T>()` are the en spellings of the outbound
# `send<T>` and inbound `recv<T>` views
#
# Common mistakes:
# • treating the words as reserved: `channel`, `frame`, `send` and `recv`
# are type names only, and stay ordinary identifiers elsewhere
#
# See also: call, channel, frame, send, recv, cursor
# =============================================================================
fn count_items(channel s) → int {
mut int n ← 0
for from s.recv<string>().cursor() let item {
print item.data
n ← n + 1
}
return n
}
main {
let channel s ← call 'runtime:echo' ("salve")
let send<value> outbound ← s.send<value>()
outbound.sink("ping")
outbound.finish()
print count_items(s)
let channel second ← call 'runtime:echo' ("vale")
let frame<string> ∪ none first ← second.recv<string>().get()
print first?.data
}Expected output:
salve
1
vale