05-代码块

Wan Yutong Lv2

Alum中,{ }的作用不再局限于创建作用域或用于包裹函数体,他可以看作一个有多个表达式组成的复合表达式,这与Rust比较类似,同时它与普通的表达式别无二致,并且可以产生值。例如:

1
2
3
4
5
let result: int = {
let n: int = 1
let m: int = 2
n + m
}

这时,result的值就是n + m,由于独立的作用域,nm{ }外不可访问。
再例如,调用函数时传入的参数也可以是由{}包裹的表达式,例如

1
2
3
4
5
F({
let a: int = 2
let b: int = 3
a * b
})

运行效果等价于F(2 * 3),但这种语法给了Alum很大的灵活性,合理使用可以提升效率,但同时也可能失去一定的可读性。

  • Title: 05-代码块
  • Author: Wan Yutong
  • Created at : 2026-02-28 17:50:50
  • Updated at : 2026-03-03 11:38:47
  • Link: https://cr0.dpdns.org/2026/02/28/05-Block/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
05-代码块