As a simple example, let us create a generic function called whoami() and some tagged methods to dispatch to
作为一个简单的例子,我们来创建一个称为whoami() 的泛型函数以及一些要被分派标记的方法
Monomorphisation specializes each use of a generic function (or structure) with specific instance,
单态化是基于函数调用(或结构的使用)的参数类型,对每个泛型函数(或结构)的特化。
'%$S': a generic function or a member-function of a generic class cannot have a variable parameter list
"%$S": 泛型函数或泛型类的成员函数不能包含变量参数列表
Rust monomorphizes generics, meaning that a new version of a generic function or type is generated for each concrete type it's used with in the program. This is similar to how templates work in C++. For example, in the following program
Rust 的单态化泛型,意味着为程序中使用的每个具体类型生成一个泛型函数或者类型的新版本。这与 C++ 中模板的工作方式类似。例如,在以下程序中:
A functor is a function that can be manipulated as an object, or an object representing a single, generic function. N/A N/A
Functor(函子)是一个函数,可以当做对象或者代表独立泛型函数的对象。 N/A N/A
It is possible to write a generic function that can generate an audit trail of the information manipulated through SQL and captured through trigger.
您可以编写这样一个泛型函数:它能够生成通过SQL操作并通过触发器捕获的信息的审计线索。
Swift function supports generics.Generic functions by a function parameter and return value is defined as a generic type, So that the function can be applied to any suitable type.The following shows a simple generic function
Swift函数支持泛型。泛型函数通过将函数参数和返回值定义为泛型类型,使得函数可以作用于任何适合的类型。下面展示了一个简单的泛型函数:
Giuseppe Castagna observed that in a typed language with multiple dispatch, a generic function can have some arguments which control dispatch and some "left-over" arguments which do not.
Giuseppe Castagna[6] 观察到在一个有类型而且有多分派的语言中,泛型函数的各个参数有些控制分派而余下那些则否。
Note: Lambda-lifting optimizations have not been applied because of the use of this local constrained generic function as a first class value. Adding type constraints may resolve this condition.
注意: 尚未应用 Lambda 提升优化,因为使用了此本地约束的泛型函数作为一类值。添加类型约束可以解析此条件。
In addition to the generic function, Swift can also customize the generic class, Generic structure and the generic generic type can be applied to any type of, Its usage and Array and Dictionary Swift offers the same.
除了泛型函数之外,Swift还可以自定义泛型类,泛型结构体和泛型枚举。这样的泛型类型可以作用于任何类型,其用法和Swift提供的 Array和Dictionary相同。
In other cases, partial application is useful when we have a very generic function and want a less universal variant of it for convenience.
另一方面,当我们有一个非常通用的函数,并希望有一个通用型更低的该函数的变体时,偏函数会非常有用。
Two distinct versions of foo will be in the final binary, one specialized to an i32 input, one specialized to a &str input. This enables efficient static dispatch of the generic function, but at the cost of a larger binary.
在最终的二进制文件中会有两个不同版本的 foo,一个专门用于接收 i32 的输入,一个专门用于接收 &str 的输入。这样实现了通用函数的高效静态调度(dispatch),但代价是会产生较大的二进制文件。
In a language with Common Lisp Object System (CLOS)-style multiple dispatch, the comparison method could be written as a generic function where both arguments are used for method selection.
在有CLOS (英语 : Common Lisp Object System ) 样式多分派特性的语言中,比较方法可以写成一个泛型方法,其两个参数类型都在方法选择中被考虑。