泛型编程沉思录—面向模版和参量化类型编程

一、泛型的概念要素:

模版类型 + 参量类型 + 类型约束 + 扩展;

泛型编程是上述四个概念反复组合而形成的一种编程范式;

 

泛型编程 = 模版类型 + 参量类型;

模版类型:与引用类型无关的通用算法和结构;

 

二、范型编程的目的:模版复用的复合类型

 产生功能模版复用的新类型;

通过类型的组合产生新的类型;

目的是通过类型组合产生新的类型;

新类型的能力由模版类型和新类型双方决定;

 

三、参量类型的选择与约束

 参量类型的选择:

任意类型;

约束类型、接口类型;

参量类型本身是否参与运算;

 

四、扩展、约束与随意多态

 随意多态 = 泛型+类型约束+扩展

 

五、泛型与系统

一个泛型模块就是一个系统;

把握一个泛型的功能从两个方面入手:

1、输入、输出;

2、类型转换;

 

 

Generic Programming is an Attitude¶

From Mathematics to Generic Programming [SR14]

Generic programming is an approach to programming that focuses on designing algorithms and data structures so that they work in the most general setting without loss of efficiency… Generic programming is more of an attitude toward programming than a particular set of tools.

In that sense, it is important to think of generic programming as an interactive approach to uncover generality without compromising performance rather than as a set of rules

As we will see, the core approach is to treat data structures and algorithms as loosely coupled, and is in direct contrast to the is-a approach of object-oriented programming

 

https://lectures.quantecon.org/jl/generic_programming.html

posted @ 2019-06-06 14:15  zzfx  阅读(205)  评论(0编辑  收藏  举报