Functions and control flow
Functions#
Faber 中的函式使用 函式 宣告,採用型別優先的參數語法,並使用字形回傳型別。
基本語法#
函式 twice(整數 n) → 整數 {
傳回 n
}帶有錯誤通道:
函式 parse(文字 input) → 整數 ⇥ 文字 {
傳回 0
}範例#
# No parameters, no return
函式 saluta() → 空值 {
註記 "Salve, Mundus!"
}
# Parameter, no explicit return
函式 dic(文字 verbum) → 空值 {
註記 verbum
}
# Parameter and return type
函式 duplica(整數 n) → 整數 {
傳回 n * 2
}
# Multiple parameters
函式 adde(整數 a, 整數 b) → 整數 {
傳回 a + b
}回傳值#
使用 傳回 進行一般回傳:
函式 porta(整數 x) → 整數 {
若 x ≺ 0 則 傳回 0
傳回 x * 2
}空值 回傳型別的裸 傳回:
函式 tace() → 空值 {
傳回
}借用與可變性(de、in、ex)#
Faber 使用參數上的簡短介系詞標記值的傳遞方式:
| 標記 | 意圖 | 常見的 Rust 降低方式 |
|---|---|---|
| (無) | 擁有值 | 以值傳遞的 T |
從 | 共用借用(唯讀) | &T |
傳入 | 可變借用 | &mut T |
取自 | 消耗(移入被呼叫者) | 以移動傳遞的 T |
# Shared borrow
functio imprime(de textus label) → vacuum {
nota label
}
# Mutable borrow
functio duplica(in numerus value) → vacuum {
value ← value * 2
}
# Consume
functio consume(ex textus buffer) → textus {
redde buffer
}
# Owned
functio salve(textus nomen) → textus {
redde "Salve, §!"(nomen)
}相同的詞(從、取自)也會在其他結構中重複使用——不要將每個 取自 都解讀為「消耗」:
| 表面語法 | 角色 |
|---|---|
參數上的 de textus name | 共用借用 |
參數上的 in numerus count | 可變借用 |
參數上的 ex textus buffer | 移入被呼叫者 |
itera ex items fixum item | 迭代值 |
itera de tabula fixum key | 迭代鍵 |
ex source fixum x, ceteri rest | 解構欄位 |
importa ex "path" | 從模組匯入 |
進入點#
程式的進入點是 入口:
入口 {
註記 "ingressus"
}CLI 進入點#
對於 CLI 程式,incipit argumenta 會接收已解析的命令列引數:
@ cli "echo"
@ descriptio "Prints text"
@ operandus ceteri textus words
incipit argumenta args {
itera ex args.words fixum word {
nota word
}
}傳遞模式——sponte#
sponte 標記可由呼叫者省略的參數:
函式 connect(文字 host, 整數 port 可選) → 空值 {
註記 host
}Control flow#
條件分支#
si / sin / secus#
入口 {
定值 布林 condition ← 真
若 condition {
# truthy branch
註記 "matched"
}
}包含 else-if 與 else:
入口 {
定值 整數 score ← 85
若 score ≥ 90 {
註記 "A"
}
否則若 score ≥ 80 {
註記 "B"
}
否則 {
註記 "C"
}
}使用 ergo 的精簡分支#
單一敘述的分支主體使用 ergo:
函式 classify(整數 b, 布林 ready, 整數 value) → 整數 ∪ 空 {
若 b ≡ 0 則 傳回 空
若 ready 則 傳回 value
傳回 空
}迭代#
值 — itera ex#
函式 inveni(列表<整數> items, 整數 target) → 整數 ∪ 空 {
遍歷 取自 items 定值 item {
若 item ≡ target 則 傳回 item
}
傳回 空
}鍵 — itera de#
入口 {
定值 JSON tabula ← { "unus": 1, "duo": 2 }
遍歷 從 tabula 定值 key {
註記 key
}
}範圍 — itera ab#
入口 {
遍歷 範圍 0‥10 定值 i {
註記 i
}
}While 迴圈#
入口 {
定值 布林 condition ← 真
當 condition {
# body
靜默
}
}保護區段 — custodi#
custodi 會在函式的主要主體之前,集中處理提早退出的檢查。
每個 若 子句都是依序執行的保護條件:
函式 divide(整數 a, 整數 b) → 整數 {
守衛 {
若 b ≡ 0 {
傳回 0
}
}
傳回 a / b
}在 v1 中,custodi 不可中斷——它是防護欄,而不是迴圈。
模式比對 — elige#
選擇 會選取第一個符合的分支:
函式 describe(整數 value) → 文字 {
比對 value {
分支 1 {
傳回 "one"
}
分支 2 {
傳回 "two"
}
分支 _ {
傳回 "many"
}
}
}標記聯集比對 — discerne#
比對 會完整比對 discretio 的各個變體:
分支聯集 Exitus {
Bonum {
文字 nuntius
},
Malum {
文字 causa
},
}
函式 refer(Exitus eventus) → 文字 {
比對 eventus {
分支 Bonum 定值 nuntius {
傳回 nuntius
}
分支 Malum 定值 causa {
傳回 "Error: §"(causa)
}
}
}Try 區塊 — fac / cape#
執行 開啟一個可能拋出例外的區塊,而 捕捉 負責復原:
函式 divide(整數 a, 整數 b) → 整數 {
傳回 a / b
}
函式 tutus(整數 a, 整數 b) → 整數 {
執行 {
傳回 divide(a, b)
}
捕捉 err {
警告 err
傳回 0
}
}Generics#
函式、型別別名、類型 與 implendum 接受使用 <T> 語法的型別參數。
泛型函式#
函式 identitas<T>(T valor) → T {
傳回 valor
}
函式 primum<T>(列表<T> res) → T ∪ 空 {
傳回 res.primus()
}明確的呼叫點型別引數#
函式 identitas<T>(T valor) → T {
傳回 valor
}
函式 primum<T>(列表<T> res) → T ∪ 空 {
傳回 空
}
定值 整數 value ← identitas<整數>(7)
定值 整數 ∪ 空 maybe ← primum<整數>([value])泛型 類型#
類型 Par<T> {
T primus
T secundus
}大小參數#
magnitudo 會在泛型參數列表中宣告大小/索引參數:
函式 crea<T, 尺寸 N>() → 張量<T, [N]> {
傳回 空集
}