泛型 Generics
# 泛型 Generics
Dafny同其他语言一样都有泛型,任何类、方法、函数都可以有类型参数,在<>中申明该数据类型T
class MyMultiset<T> {
/*...*/
} //类泛型
datatype Tree<T> = Leaf | Node(Tree<T>, T, Tree<T>) //自定义数据泛型
method Find<T>(key: T, collection: Tree<T>) { //方法泛型
/*...*/
}
function IfThenElse<T>(b: bool, x: T, y: T): T { //函数泛型
/*...*/
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
编辑 (opens new window)
上次更新: 2022/03/26, 14:38:56