函数类型签名
A 函数类型签名 描述了一个函数的输入参数和类型,以及函数的输出类型。使用类型签名来识别函数参数期望的数据类型,并理解函数的预期输出。
函数类型签名结构
(parameter: type) => output-type
参数符号
参数符号表示函数参数的特定行为。
? // Optional parameter
<- // Pipe receive – indicates the parameter that, by default, represents
// the piped-forward value
类型变量
Flux 类型签名使用 类型变量 来表示签名中的唯一类型。类型变量是 多态的,这意味着它可以是多种类型之一,并且可能受到 类型约束 的限制。
类型变量使用以下标识符模式:
A
B
C
t11
// etc.
类型符号
流类型
类型签名使用 stream[A] 语法来识别流类型 (表的流),其中 A 是一个唯一的 类型变量。流类型可以包含特定的列名和列类型。
// Stream of tables
stream[A]
// Stream of tables with specific columns, but inferred column types.
stream[{col1: A, col2: B}]
// Stream of tables additional or required "count" column with an
// explicit integer type.
stream[{A with count: int}]
基本类型
类型签名通过以下类型标识符标识基本类型:
bool // boolean type
bytes // bytes type
duration // duration type
float // float type
int // integer type
regexp // regular expression type
string // string type
time // time type
uint // unsigned integer type
复合类型
类型签名识别 Flux 复合类型,其语法如下:
[A] // array type
[B: A] // dictionary type
(param: A) => B // function type
{_value: int} // record type
类型约束
某些函数参数是“多态的”,可以支持多种数据类型。
多态参数受类型约束的限制,这些约束定义了可以使用哪些类型。
类型签名使用where A: Constraint语法指示特定值的类型约束。
例如,以下类型签名描述了一个接受单个参数的函数,v 并返回一个整数。v 可以是满足 Timeable 约束的任何类型(持续时间或时间)。
(v: A) => int where A: Timeable
有关不同类型约束及其支持的类型的更多信息,请参见 Type constraints。
示例函数类型签名
无参数的函数
以下类型签名描述了一个函数:
- 没有参数
- 返回一个时间值
() => time
带参数的函数
以下类型签名描述了一个函数:
- 具有两个类型为
A的参数:- multiplier (可选)
- v (必需)
- 返回与两个输入参数相同类型的值
(?multiplier: A, v: A) => A
透传转换
以下类型签名描述了一个 转换:
- 接受类型为
A的表格流作为管道输入 - 返回未修改类型的表的输入流
(<-tables: stream[A]) => stream[A]
基本转换
以下类型签名描述了一个 转换,它:
- 接受类型为
A的表流作为前置输入 - 具有一个
fn参数,其类型为函数fn使用类型A作为输入并返回类型B
- 返回一个新的、修改过的类型为
B的表流
(<-tables: stream[A], fn: (r: A) => B,) => stream[B]
添加具有显式类型的列的转换
以下类型签名描述了一种 转换,它:
- 接受类型为
A的表流作为管道输入 - 具有一个必需的 tag 参数,类型为
BB类型受限于 Stringable 约束
- 返回一个新的、修改过的类型为
A的表流,其中包含一个 tag 列,值为字符串
(<-tables: stream[A], tag: B) => stream[{A with tag: string}] where B: Stringable