当前位置: 首页 > news >正文

golang generic 2022-04-13

在代码不关注具体的数据类型而关注逻辑本身时,我们通常希望使用一个通用的模版——泛型generic。例如:实现一个通用的比大小方法,关注的是比较算法本身,而非比较的对象的类型,不管是小数、整数还是复数
泛型的顶层设计是对类型进行参数化
泛型是为了减少程序员的负担,编写程序更灵活方便。但这必然在编译时或者运行时增加了复杂度

java 泛型实现——“装箱”

先简单了解java的泛型实现

“装箱”,就是擦除原类型,统一类型,参数装到Object箱子里,就都变成了Object实例

一个泛型类的所有实例在运行时具有相同的运行时类(class),而不管他们的实际类型参数,如下例所示

List<String> l1 = new ArrayList<String>();
List<Integer> l2 = new ArrayList<Integer>();
System.out.println(l1.getClass() == l2.getClass()); // true

这是为什么呢?也是装箱的缘故
因为java的泛型是通过编译器擦除泛型的类型信息实现的,比如说一个List<String>类型被转换为List<Object>,一个List<Integer>也会被转换为List<Object>。所有对类型变量的引用被替换成类型变量的上限(通常是Object),换句话说,所有的参数都由Integer、String等具体类型的实例对象转换成统一的Object对象——装箱
编译器会将泛型函数转换成不带任何类型参数的具体实现,类型参数在运行时并不存在,因此泛型类的实例在运行时具有相同的运行时类。注意,这样一来也不能依靠类型参数进行类型转换,类型参数在编译阶段就被干掉了
如,泛型函数badCast不能依靠类型参数T做类型转换

  <T> T badCast(T t, Object o) {return (T) o; // unchecked warning}

java中类的静态变量和方法在所有的实例间共享,这就是为什么在静态方法或静态初始化代码中或者在静态变量的声明和初始化时使用类型参数(类型参数是属于具体实例的)是不合法的原因

golang 泛型实现

generic functions and generic types

// generic functions
func f [T1, T2 any](x int, y T1) T2 {...
}// generic types Vector is a name for a slice of any element type.
type Vector[T any] []T

几个相关markdown

  • https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md
  • https://github.com/golang/proposal/blob/master/design/generics-implementation-stenciling.md
    stencile 钢印,在编译时为泛型支持的每一种类型都生成一份函数实例
type Op interface{int|float
}
func Add[T Op](m, n T) T {return m + n
}
// 生成后 =>
func Add[go.shape.int_0](m, n int) int{}
func Add[go.shape.float_0](m, n float) float{}

也叫单态化,很好理解

  • https://github.com/golang/proposal/blob/master/design/generics-implementation-dictionaries.md
    当为泛型函数生成实例的时候,会唯一生成一个实例函数。该实例函数会擦除泛型函数的类型信息,确保传递给实例函数的参数都是统一类型的通用对象,java的Object、golang的interface{}都是统一类型;同时新增一个指向字典类型的指针(*dictionary)作为参数
type dictionary struct {T1 *runtime._typeT2 *runtime._type...
}

泛型函数f的dictionary需要包含如下信息:

  • The first thing that the dictionary will contain is a reference to the runtime._type for each parameterized type
  • Contain a *runtime._type for each of the types mentioned in the body of f which are derived from the generic parameter types.
  • Subdictionaries. If generic_f calls other functions, it needs a dictionary for those calls.
  • Helper methods. The dictionary should contain methods that operate on the generic types.
  • Stack layout. f needs to allocate stack space for any temporary variables it needs. Some of those variables would be of generic type, so f doesn’t know how big they are. It is up to the dictionary to tell it that.
  • Pointer maps. Each stack frame needs to tell the runtime where all of its pointers are.

总之,dictionary包含了原始的类型信息、方法信息、子字典等元数据,使得泛型函数实例在执行内部逻辑时可以“有法可依”。dictionary提供了执行依据

golang综合使用了stencile和dictionary方法来实现泛型,见下:
https://github.com/golang/proposal/blob/master/design/generics-implementation-gcshape.md
概括起来就是:

  • 采用模版印刷的方式为具有相同gcshape的类型复制一份模版函数。
  • 泛型函数调用时,都会增加一个参数,用来传递字典,由编译器在编译阶段添加,用户无感知。
  • 使用字典来区分相同gcshape类型的不同行为 。


喜欢的朋友记得点赞、收藏、关注哦!!!

http://www.dtcms.com/a/264060.html

相关文章:

  • Linux 系统重启 reboot与重置reset深度解析
  • 【读代码】百度开源大模型:ERNIE项目解析
  • 软件测试复习之单元测试
  • C#系统学习第六章——循环语句
  • 【PDF-XSS攻击】springboot项目-上传文件-解决PDF文件XSS攻击
  • 创始人IP商业闭环构建:从定位到二次转化的全流程|创客匠人
  • 【文件解析】json.load(fp)
  • 数据结构——单链表反转、相邻节点最大值、有序链表合并
  • 【javaAI】SpringAI快速入门
  • Kafka日常运维命令总结
  • 第4课:Flask请求与响应对象深度解析
  • 【Python】Flask网页
  • React Native 0.79.4 中 [RCTView setColor:] 崩溃问题完整解决方案
  • JavaEE初阶第六期:解锁多线程,从 “单车道” 到 “高速公路” 的编程升级(四)
  • 无法将“pytest”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
  • NLP——RNN变体LSTM和GRU
  • 【Linux】进程
  • ELK日志分析系统(filebeat+logstash+elasticsearch+kibana)
  • Pycharm安装第三方库
  • 【实战】 容器中Spring boot项目 Graphics2D 画图中文乱码解决方案
  • 脑机新手指南(二十一)基于 Brainstorm 的 MEG/EEG 数据分析(上篇)
  • ChatGPT + GitHub Copilot + Cursor 实战提升编程效率
  • Oracle 常用函数
  • WPF中Style和Template异同
  • 【CodeTop】每日练习 2025.7.1
  • 使用 Conda 工具链创建 Poetry 本地虚拟环境全记录——基于《Python 多版本与开发环境治理架构设计》
  • 迅为高情性6TOPS算力的RK3576开发板NPU rknn-model-zoo例程演示
  • Windows VMWare Centos Docker部署Springboot + mybatis + MySql应用
  • Windows版minio下载安装使用教程
  • 最大子数组和-力扣