ช่องทาง
Translation status: ภาษาไทย reader-locale proof. Term names and code fences follow the th-TH pack; supporting prose may still be English.
A ช่องทาง 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.
Syntax: คงที่ ตรรกะ clausa ← s.close()
Category#
endpoint
Related#
- ถึง
- sermo
- scrinium
- ส่ง
- รับ
- เคอร์เซอร์
Examples#
radix/corpus/ad/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 `ช่องทาง`, the handle `call` returns
# • `frame<T>` is the en spelling of `เฟรม<T>`, one typed frame
# • `s.send<T>()` / `s.recv<T>()` are the en spellings of the outbound
# `ส่ง<T>` and inbound `รับ<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: ถึง, ช่องทาง, เฟรม, ส่ง, รับ, เคอร์เซอร์
# =============================================================================
fn count_items(channel s) → int {
var int n ← 0
for from s.recv<string>().cursor() const item {
print item.data
n ← n + 1
}
return n
}
main {
const channel s ← call 'runtime:echo' ("salve")
const send<value> outbound ← s.send<value>()
outbound.da("ping")
outbound.fini()
print count_items(s)
const channel second ← call 'runtime:echo' ("vale")
const frame<string> ∪ none first ← second.recv<string>().accipe()
print first?.data
}Expected output:
salve
1
vale
radix/corpus/ad/sermo-close.fab (canonical · method)#
A ช่องทาง 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.
# =============================================================================
# ช่องทาง — a conversation closes itself when it completes; `close()` is early.
# =============================================================================
#
# What this teaches:
# • Completion closes — when the handler returns (or fails), the ช่องทาง
# releases its handler task and router slot at once, even while its
# handle still sits in a ตาราง; the handle keeps reading as its result
# • A pool of many — finished conversations cost nothing while the pool
# waits on the rest, so a ตาราง 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 `⇥ ข้อความ`; it returns `จริง` when
# this call closed the handle
# • Last reference — a ช่องทาง 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 ช่องทาง 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: ถึง, ช่องทาง, รับ, ทำ, จับ
# =============================================================================
@ ถึง 'numeri:bis'
ฟังก์ชัน bis(จํานวน n) → จํานวน {
คืน n * 2
}
@ ถึง 'numeri:numera'
ฟังก์ชัน numera(จํานวน n) สตรีม → จํานวน {
วน ช่วง 0‥n คงที่ i {
ให้ i
}
}
@ ถึง 'numeri:defice'
ฟังก์ชัน defice(จํานวน n) → จํานวน ⇥ ข้อความ {
โยน "defecit §"(n)
}
ฟังก์ชัน ผลรวม(รายการ<จํานวน> partes) → จํานวน {
แปร จํานวน totum ← 0
วน ออก partes คงที่ pars {
totum ← totum + pars
}
คืน totum
}
เริ่ม {
# Four conversations finish at once; their handles wait in the pool.
แปร ตาราง<จํานวน, ช่องทาง> piscina ← เซตว่าง
วน ช่วง 0‥4 คงที่ i {
piscina[i] ← ถึง 'numeri:bis' (i)
}
# A longer conversation streams while the finished ones hold nothing.
คงที่ ช่องทาง longus ← ถึง 'numeri:numera' (5)
วน ช่วง 0‥4 คงที่ i {
บันทึก piscina[i] ↦ จํานวน
}
บันทึก ผลรวม(longus ↦ รายการ<จํานวน>)
# Reading waits for the handler (a failed read defaults to 0); close()
# then reports the error the conversation ended with.
คงที่ ช่องทาง เศษ ← ถึง 'numeri:defice' (7)
บันทึก เศษ ↦ จํานวน ⊥ 0
ทำ {
คงที่ ตรรกะ clausa ← fractus.close()
บันทึก clausa
} จับ err {
บันทึก err
}
# Closing early drops the unread reply; a live handler is cancelled.
ทำ {
คงที่ ช่องทาง ultimus ← ถึง 'numeri:bis' (21)
คงที่ ตรรกะ clausa ← ultimus.close()
บันทึก clausa
} จับ err {
บันทึก err
}
}Expected output:
0
2
4
6
10
0
defecit 7
verum