类型
类型 定义了一组值以及对这些值的操作。类型永远不会作为语法的一部分显式声明,除非作为 内置语句 的一部分。类型始终从值的用法中推断出来。类型推断遵循 Hindley-Milner 风格的推断系统。
联合类型
联合类型定义了一组类型。在下面的示例中,联合类型指定如下
T = t1 | t2 | ... | tn
其中 t1
、t2
、… 和 tn
是类型。
在上面的示例中,类型为 T
的值可以是类型 t1
、类型 t2
、… 或类型 tn
。
基本类型
所有 Flux 数据类型都由以下类型构建
Null 类型
null 类型 表示缺失值或未知值。null 类型 名称为 null
。 null 类型 只有一个值,即 null 值。如果类型 t
可以表示如下,则它是可为空的
t = {s} | null
其中 {s}
定义了一组值。
Boolean 类型
布尔类型 表示真值,对应于预先分配的变量 true
和 false
。布尔类型名称为 bool
。布尔类型是可为空的,可以正式指定如下
bool = {true, false} | null
数值类型
数值类型 表示整数或浮点值集。
存在以下数值类型
uint the set of all unsigned 64-bit integers | null
int the set of all signed 64-bit integers | null
float the set of all IEEE-754 64-bit floating-point numbers | null
所有数值类型都是可为空的。
Time 类型
时间类型 表示纳秒精度的单个时间点。时间类型名称为 time
。时间类型是可为空的。
时间戳格式
Flux 支持 RFC3339 时间戳
YYYY-MM-DD
YYYY-MM-DDT00:00:00Z
YYYY-MM-DDT00:00:00.000Z
Duration 类型
持续时间类型 表示纳秒精度的时间长度。持续时间类型名称为 duration
。持续时间类型是可为空的。
持续时间类型示例
1ns // 1 nanosecond
1us // 1 microsecond
1ms // 1 millisecond
1s // 1 second
1m // 1 minute
1h // 1 hour
1d // 1 day
1w // 1 week
1mo // 1 calendar month
1y // 1 calendar year
3d12h4m25s // 3 days, 12 hours, 4 minutes, and 25 seconds
String 类型
字符串类型 表示可能为空的字符序列。字符串是不可变的,一旦创建就无法修改。字符串类型名称为 string
。字符串类型是可为空的。
空字符串不是 null 值。
Bytes 类型
字节类型 表示字节值序列。字节类型名称为 bytes
。
Regular expression 类型
正则表达式类型 表示正则表达式的所有模式的集合。正则表达式类型名称为 regexp
。正则表达式类型不可为空。
复合类型
这些是从基本类型构造的类型。复合类型不可为空。
Array 类型
数组类型 表示任何其他类型的值序列。数组中的所有值必须是相同的类型。数组的长度是数组中元素的数量。
Record 类型
记录类型 表示一组无序的键值对。键必须始终是字符串。值可以是任何其他类型,并且不需要与记录中的其他值相同。
记录中的键只能静态引用。
类型推断确定记录中存在的属性。如果类型推断确定了记录中的所有属性,则称其为“有界的”。并非记录类型中的所有键都是已知的,在这种情况下,该记录被称为“无界的”。除了已知包含的属性外,无界记录还可以包含任何属性。
Dictionary 类型
字典类型 是将键与值关联的集合。键必须是可比较的且类型相同。值也必须是相同的类型。
Function 类型
函数类型 表示具有相同参数和结果类型的所有函数的集合。函数参数始终是命名的(没有位置参数)。因此,实现函数类型要求参数名称相同。
Stream 类型
流类型 表示无界的值集合。这些值必须是记录,并且这些记录只能包含 int、uint、float、string、time 或 bool 类型。
多态
Flux 函数可以是多态的,这意味着一个函数可以应用于不同类型的参数。Flux 支持参数多态、记录多态和特设多态。
参数多态
参数多态的概念是,一个函数可以统一地应用于任何类型的参数。例如
f = (x) => x
f(x: 1)
f(x: 1.1)
f(x: "1")
f(x: true)
f(x: f)
在 add
函数体中,标识符 a
和 b
同时用作 int
和 float
类型。
记录多态
记录多态的概念是,一个函数可以应用于不同类型的记录。例如
john = {name:"John", lastName:"Smith"}
jane = {name:"Jane", age:44}
// John and Jane are records with different types.
// We can still define a function that can operate on both records safely.
// name returns the name of a person
name = (person) => person.name
name(person:john) // John
name(person:jane) // Jane
device = {id: 125325, lat: 15.6163, lon: 62.6623}
name(person:device) // Type error, "device" does not have a property name.
只要记录包含必要的属性,就可以将不同类型的记录传递给同一个函数。必要的属性由记录的使用方式决定。
特设多态
特设多态的概念是,一个函数可以应用于不同类型的参数,并且根据类型具有不同的行为。
add = (a, b) => a + b
// Integer addition
add(a: 1, b: 1)
// String concatenation
add(a: "str", b: "ing")
// Addition not defined for boolean data types
add(a: true, b: false)
类型约束
类型约束是为了实现静态特设多态。例如,以下函数仅为 Addable
类型定义
add = (a, b) => a + b
将记录传递给 add()
会导致编译时类型错误,因为记录不可加。
// Records are not Addable and will result in an error.
add(a: {}, b: {})
约束永远不会显式声明,而是从上下文中推断出来。
Addable 约束
Addable 类型是二进制算术运算符 +
接受的类型。Integer、Uinteger、Float 和 String 类型是 Addable
。
Subtractable 约束
Subtractable 类型是二进制算术运算符 -
接受的类型。Integer、Uinteger 和 Float 类型是 Subtractable
。
Divisible 约束
Divisible 类型是二进制算术运算符 \
接受的类型。Integer、Uinteger 和 Float 类型是 Divisible
。
Numeric 约束
Integer、Uinteger 和 Float 类型是 Numeric
。
Comparable 约束
Comparable 类型是二进制比较运算符 <
、<=
、>
或 >=
接受的类型。Integer、Uinteger、Float、String、Duration 和 Time 类型是 Comparable
。
Equatable 约束
Equatable 类型是可以使用 ==
或 !=
运算符比较是否相等的类型。Boolean、Integer、Uinteger、Float、String、Duration、Time、Bytes、Array 和 Record 类型是 Equatable
。
Nullable 约束
Nullable 类型是可以为 null 的类型。Boolean、Integer、Uinteger、Float、String、Duration 和 Time 类型是 Nullable
。
Record 约束
记录是唯一属于此约束的类型。
Negatable 约束
Negatable 类型是一元算术运算符 -
接受的类型。Integer、Uinteger、Float 和 Duration 类型是 Negatable
。
Timeable 约束
Duration 和 Time 类型是 Timeable
。
Stringable 约束
Stringable 类型是可以评估并在字符串插值中表示的类型。String、Integer、Uinteger、Float、Boolean、Time 和 Duration 类型是 Stringable
。
此页是否对您有帮助?
感谢您的反馈!