Learn Aero / Variables & Types

Variables & Types

Declaring Variables

Aero declares variables with let; the type can be inferred:

let x = 1;
print(x);   // 输出 1

Four basic types:

i6464-bit signed integer42, -7
i3232-bit signed integerintegers, decided by annotation
boolbooleantrue, false
strstring"hello"

The Important Rule of Type Inference

Integer literals are "flexible":

let x = 1;          // x 是 i64
let y: i32 = 1;     // 合法!字面量 1 可以适配 i32
let z: i32 = x;     // 报错!x 已经是 i64,塞进 i32 是隐式窄化

Rule: once a type is determined, it will never be narrowed again. Implicit narrowing is the most common source of bugs in C/C++, and Aero chooses to stop it at compile time.

Explicit Annotations

let n: i64 = 10000000000;   // 十亿级,必须 i64
let small: i32 = 5;
let flag: bool = true;
let name: str = "Aero";

Variables Can Be Reassigned

let x = 1;
x = x + 1;
print(x);   // 输出 2

On assignment the new value's type must match the original type; narrowing is not allowed.

Block Scoping

let a = 1;
if (true) {
    let b = 2;
    print(a + b);   // 输出 3
}
// print(b);        // 错误:b 在这里不存在

Declaring two variables with the same name in the same scope is not allowed.

No const

Aero 0.1 has no const; every variable can be reassigned. If you want "read-only", rely on convention rather than compiler enforcement.

Aero 文档 · 基于 Aero 1.2.0 文档整理 · MIT
SereinTeam