渲染zh-Hans

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#

入口 {
    遍历 范围 010 常量 i {
        显示 i
    }
}

while 循环#

入口 {
    常量 布尔 condition  
     condition {
        # body
        静默
    }
}

守卫段 —— custodi#

custodi 用于在函数主体之前组织提前退出检查。 每一条 如果 子句都是一个顺序守卫:

函数 divide(整数 a, 整数 b)  整数 {
    守护 {
        如果 b  0 {
            返回 0
        }
    }
    返回 a / b
}

custodi 在 v1 中不可被 break —— 它是护栏,不是循环。

模式匹配 —— 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])

泛型 genus#

 Par<T> {
    T primus
    T secundus
}

尺寸参数#

magnitudo 在泛型参数列表中声明一个尺寸/索引参数:

函数 crea<T, 维度 N>()  张量<T, [N]> {
    返回 空集
}