函数类型签名
函数类型签名描述了函数的输入参数和类型,以及函数的输出类型。使用类型签名来识别函数参数期望的数据类型,并了解函数的预期输出。
函数类型签名结构
(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 约束(duration 或 time)的任何类型。
(v: A) => int where A: Timeable
有关不同类型约束以及每种约束支持的类型的更多信息,请参阅类型约束。
函数类型签名示例
无参数函数
以下类型签名描述了一个函数,该函数:
- 没有参数
- 返回一个时间值
() => 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
的表流作为管道输入 - 有一个类型为
B
的必需 tag 参数B
类型受 Stringable 约束的约束
- 返回一个新的、修改后的类型为
A
的表流,其中包含一个带有字符串值的 tag 列
(<-tables: stream[A], tag: B) => stream[{A with tag: string}] where B: Stringable
此页面是否对您有帮助?
感谢您的反馈!