整数与浮点数
与C或C++不同,lelang要求显示指定整数与浮点数的长度,lelang保证在任何实现lelang编译器的平台上均为同样的长度。
整数共有8种类型
- i8~i32
- u8~u32
浮点数共有2种类型
- f32
- f64
Tips: lelang禁止不同类型的整数或浮点数发生隐式转换,例如下面的代码无法通过编译
decl le print_int64(i64);
le main()->i32{
var width_64:i32 = 100;
print_int64(width_64);
ret 0;
}
编译它,我们得到👉🏻
[E0014] Error: expect a type `i64`, but got `i32`
╭─[docs/src/test_sources/invalid_conversion.le:6:16]
│
6 │ print_int64(width_64);
· ─────┬─────
· ╰─────── expect type `i64`, but found type `i32`
·
· Help: maybe you need a type cast to type `i64` ?`
───╯
同样,不同类型的整数或浮点数也不能直接相加
le main()->i32{
var width_32:i32 = 100;
var width_64_float:f64 = 10.33;
width_32=width_32+width_64_float;
ret 0;
}
编译它,我们得到👉🏻
[E0012] Error: no suitable binary operator `+` for type: `i32` and `f64`
╭─[docs/src/test_sources/invalid_conversion1.le:5:14]
│
5 │ width_32=width_32+width_64_float;
· ───────────┬───────────
· ╰───────────── operator `+` not suitable for type `i32` and type `f64` here
·
· Help: maybe you need a `as` type cast here?
───╯