渲染zh-Hant

通道

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant 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

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