混合类型规则
语言没有通用的隐式转换。每个运算符只认识一小组类型组合。其他任何组合都不会引发错误,而是静默地得到 Integer 0(对于大小比较则为 false),脚本继续运行。这是有意为之,以免一个异常值让操作半途中止。但这也意味着写错类型的表达式会静默失败,因此当结果看起来不对时,请用 UtilityPrint 检查。
| 运算符 | 接受的组合 → 结果 | 其他组合 |
|---|---|---|
+ | Text + Text → Text(连接) Integer + Integer → Integer Integer/Real 混合 → Real | 0 |
- * | Integer ∘ Integer → Integer Integer/Real 混合 → Real | 0 |
/ | Integer / Integer → Integer,向零截断(-7 / 2 为 -3)Integer/Real 混合 → Real 除以零 → 0 或 0.0,不报错 | 0 |
== != | 相同类型 → 按值比较(Text 精确比较且区分大小写,Window 按句柄比较) Integer 与 Real → 按数值比较( 5 == 5.0) | 永不相等:1 == true、"5" == 5 和 w == 0 均为 false |
< <= > >= | 两个数字(允许 Integer/Real 混合)→ 数值顺序 Text 与 Text → 序数比较,区分大小写(按 UTF-16 代码单元: "Z" < "a") | 四个运算符均为 false,因此在这种情况下 !(a < b) 并不等同于 a >= b |
&& || ! | 任何类型,按真值判断 → Bool | |
& | ^ | Integer ∘ Integer → Integer | 0(Bool 或 Real 操作数也算在内:true & true 为 0) |
<< >> | Integer 按 0–63 的 Integer 移位 → Integer。>> 保留符号:-16 >> 2 为 -4。 | 0,包括移位量小于 0 或大于 63 的情况 |
一元 - | Integer → Integer,Real → Real | 0 |
~ | Integer → Integer(~0 为 -1) | 0 |
类型之间的转换:
| 从 → 到 | 方法 |
|---|---|
| Integer → Real | 传给声明为 Real 的内置函数参数时自动转换;其他情况使用 n * 1.0(任何涉及 Real 的算术运算都得到 Real) |
| Real → Integer | MathRound(r)(中间值远离零舍入)、MathFloor(r) 或 MathCeil(r) |
| 任意类型 → Text | StringFormat("{0}", value, "") |
| Bool → Integer | if (b) n = 1; else n = 0; |
| Text → 数字 | 对用户键入的内容使用 StringToNumber(text, false)(按其区域格式;数字分组必须符合该格式,因此对于中文(简体,中国)用户,1,5 不是数字),对机器生成的文本使用 true(. 作小数点,无分组符)。为整数时返回 Integer,否则返回 Real;如果文本可能不是数字,请先使用 StringIsNumber(参阅此示例)。 |
| 数字 → Text | StringFromNumber(n, decimals, invariantCulture),decimals 为 -1 时按需显示位数 |
| Integer → 十六进制 Text | 没有内置函数能做到这一点。请用移位来构造(参阅此示例)。 |