What is Generic Mechanism

面向对象的一个重要目标就是对代码复用的支持。支持这个目标的一个重要机制就是
泛型机制(generic mechanism): 如果除去对象的基本类型外,实现方法是相同的,那么我们就可以用泛型实现(generic implementation) 来描述这种基本的功能。

GOlang 在1.18之后的版本开始支持泛型了.
函数要支持泛型机制,需要有2个前提:

  1. 对于函数而言,需要一种方式来声明这个函数到底支持哪些类型的参数
  2. 对于调用者而言,需要一种方式去指定给函数传递的参数是什么类型

为了满足以上前提条件:

  1. 在声明函数的时候,除了需要像普通函数一样添加函数的形式参数之外, 还要声明这些形参的类型决定因素(type parameters), 从而让函数支持泛型机制,处理不同类型的参数
  2. 在函数调用时,除了像普通函数一样传递实参之外,还需要传递泛型函数的类型决定因素的对应类型实参(type arguments)

每个类型决定因素都有类型约束(type contraint), 有点像是类型决定因素的元类型(meta-type), 即约束类型决定因素可以传入什么类型的类型实参(e.g. int string float etc.)

注意:一定要确保泛型实现代码里对类型决定因素所做的所有操作都是合法的。例如对一个包含了string类型约束的类型决定因素进行取下标操作,如果这时类型约束还包括了int等数字类型的话,编译器就会报错,从而编译失败。

Generic Implementation

泛型函数实现

  1. 使用类型约束来允许value为int类型和float类型的map作为函数的类型实参.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    // SumIntsOrFloats sums the values of map m. It supports both int64 and float64
    // as types for map values.
    func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
    var s V
    for _, v := range m {
    s += v
    }
    return s
    }

在上面的代码中,我们做了如下事情:

  • 声明函数SumIntsOrFloats, [] 中的K V 作为类型决定因素, 一个函数形参m, 类型为map[K]V , 函数返回值的类型为V
  • 类型决定因素K 的类型约束为comparablecomparable 是golang新引入的预定义标识符,是一个接口,指代可以使用==!= 来进行比较的类型集合。GOlang中map的key的类型决定因素必须是comparable的,这确保了调用方使用合法的类型作为map的key。

    glang spec对map的key有如下约束:

    • key类型必须定义比较操作符==!=
    • key类型必须不能是functionmapslice(没有定义比较操作符)
    • 对于interface类型,其动态类型必须定义比较操作符
    • 不满足上述约束,则会导致运行时异常(run-time panic)
    • 类型决定因素V的类型约束为int64float64 。使用| 表示取并集, 即int64float64 的任意一个都可以满足类型约束,作为函数传入的类型实参。
    • 函数参数m的类型是map[K]V。因为Kcomparable类型, 所以map[K]V 是一个合法的map类型。如果K没有声明为comparable, 则编译器会拒绝对map[K]V 的引用。
  1. main 函数中调用上面的泛型实现

    在调用泛型函数时,可以通过[] 传入类型实参给类型决定因素。
    也可省略掉类型实参,让编译器根据函数调用时传入的函数实参类型自动推导出来, 从而让代码更加简洁。
    注意:类型实参的自动推导并不是永远可行的。比如你调用的泛型函数没有形参,不要传递实参,那编译器就不能自动推导,需要在[] 中显示指定类型实参。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func main() {
// Initialize a map for the integer values
ints := map[string]int64{
"first": 34,
"second": 12,
}

// Initialize a map for the float values
floats := map[string]float64{
"first": 35.98,
"second": 26.99,
}
fmt.Printf("Generic Sums: %v and %v\n",
SumIntsOrFloats[string, int64](ints),
SumIntsOrFloats[string, float64](floats))

fmt.Printf("Generic Sums, type parameters inferred: %v and %v\n",
SumIntsOrFloats(ints),
SumIntsOrFloats(floats))
}

运行结果:

1
2
3
4
Generic Sums: 46 and 62.97
Generic Sums, type parameters inferred: 46 and 62.97

Program exited.

Declaration type contraint

把泛型函数里的类型约束以接口(interface)的形式做定义,这样类型约束就可以在很多地方被复用。声明类型约束可以帮助精简代码,特别是在类型约束很复杂的场景下。

我们可以声明一个类型约束(type constraint)为接口(interface)类型。这样的类型约束可以允许任何实现了该接口的类型作为泛型函数的类型实参。例如,你声明了一个有3个方法的类型约束接口,然后把这个类型约束接口作为泛型函数的类型决定因素的类型,那函数调用时的类型实参必须要实现了接口里的所有方法。

类型约束接口也可以指代特定类型,在下面大家可以看到具体使用。
把原本来函数声明里的 int64float64的并集改造成了一个新的类型限制接口Number,当我们需要限制类型参数为int64float64时,就可以使用Number这个类型限制来代替int64 | float64的写法。

1
2
3
4
5
6
7
8
9
10
11
12
type Number interface {
int64 | float64
}
// SumNumbers sums the values of map m. Its supports both integers
// and floats as map values.
func SumNumbers[K comparable, V Number](m map[K]V) V {
var s V
for _, v := range m {
s += v
}
return s
}

main函数中添加以下代码

1
2
3
fmt.Printf("Generic Sums with Constraint: %v and %v\n",
SumNumbers(ints),
SumNumbers(floats))

运行结果如下

1
2
3
4
5
Generic Sums: 46 and 62.97
Generic Sums, type parameters inferred: 46 and 62.97
Generic Sums with Constraint: 46 and 62.97

Program exited.

Generics tricks

对任何类型都返回零值

你经常会写一些返回 anyerror的代码,比如说下面这样

1
2
3
4
5
6
7
8
func Do[V any](v V) (V, error) {
if err := validate(v); err != nil {
// What should we return here?
}
return v, nil
}

func validate[V any](v V) error { return nil }

假设你在这里写return 0, err。这将是一个编译错误。原因是any类型可以是int类型以外的类型,比如string类型。那么我们应该怎么做呢?

让我们用类型参数的V声明一次变量。然后你可以把它写成可编译的形式,如下:

1
2
3
4
5
6
7
func Do[V any](v V) (V, error) {
var ret V
if err := validate(v); err != nil {
return ret, err
}
return v, nil
}

此外,可以使用带命名的返回值来简化单行的书写。

1
2
3
4
5
6
func Do[V any](v V) (ret V, _ error) {
if err := validate(v); err != nil {
return ret, err
}
return v, nil
}

利用泛型定义python里的字典

在golang中map被定义为一种由键(key)及索引值(value)构成的元素集合,实质上与python中Dictionary表达的是同一个东西。考虑到函数式编程中经常使用map来表示映射,在有些场景下我们可以通过将golang中的map重定义为Dictionary来减少概念上的混淆。

1
2
3
4
5
6
7
8
9
10
package main

import "fmt"

type Dictionay[K comparable, V any] map[K]V

func main() {
dict := Dictionay[string, int]{"string": 1}
fmt.Printf("dict: %#v \n", dict)
}

利用泛型定义python里的集合Set

Golang并未提供内置的Set类型,不过一般我们可以利用map的key的唯一性来自定义Set类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package sets

// Set is a set of elements
type Set[T comparable] map[T]struct{}

// NewSet returns a set of elements with assigned type
func NewSet[T comparable](es ...T) Set[T] {
s := Set[T]{}
for _, e := range es {
s.Add(e)
}
return s
}

// Len report the elements number of s
func (s *Set[T]) Len() int {
return len(*s)
}

// IsEmpty report wether s is empty
func (s *Set[T]) IsEmpty() bool {
return s.Len() == 0
}

// Add add elements to set s
// if element is already in s this has no effect
func (s *Set[T]) Add(es ...T) {
for _, e := range es {
(*s)[e] = struct{}{}
}
}

// Remove remove elements from set s
// if element is not in s this has no effect
func (s *Set[T]) Remove(es ...T) {
for _, e := range es {
delete(*s, e)
}
}

// Contains report wether v is in s
func (s *Set[T]) Contains(v T) bool {
_, ok := (*s)[v]
return ok
}

// Clone create a new set with the same elements as s
func (s *Set[T]) Clone() Set[T] {
r := Set[T]{}
r.Add(s.ToSlice()...)
return r
}

// ToSlice transform set to slice
func (s *Set[T]) ToSlice() []T {
r := make([]T, 0, s.Len())

for e := range *s {
r = append(r, e)
}

return r
}

// Union the union of some sets(eg. A, B)
// is the set of elements that are in either A or B
func Union[T comparable](sets ...Set[T]) Set[T] {
r := NewSet[T]()
for _, s := range sets {
r.Add(s.ToSlice()...)
}
return r
}

RERFERENCES

[1] https://go.dev/doc/tutorial/generics
[2] YuanJianzheng. https://juejin.cn/post/7055992040068218888
[3] https://zhuanlan.zhihu.com/p/438252333