类型转换

Dujie 当前只支持少量内建显式转换。

转换语法

使用 Type(expr)

let a = float(3);
let b = int(3.14);

当前支持

只支持:

  • float(int_expr)
  • int(float_expr)

其中 int(float_expr) 按向零截断处理。

当前不支持

  • int / float 混合运算自动提升
  • string(123) 这类任意文本化
  • int("123") 这类字符串解析
  • bool(...)
  • rune(...)
  • any -> T 的显式转换

文本化

如果需要得到字符串,当前更推荐:

  • 使用插值字符串
  • 或使用标准库中的格式化能力(后续补齐)

例如:

let x = 123;
let s = `${x}`;

any

any 不通过类型转换恢复为具体类型,而是通过 is 做显式收窄:

let value = props["title"];

if value is string(s) {
    text(s)
}