当前位置: 首页 > 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.lryc.cn/news/578584.html

相关文章:

  • 技术学习_人工智能_1_神经网络是如何实现的?
  • IDE全家桶专用快捷键----------个人独家分享!!
  • 02.SpringBoot常用Utils工具类详解
  • pytorch学习—7.处理多维特征的输入
  • 通达信【极弱强势指标与股道波段交易系统】幅图
  • 【学习笔记】Python中主函数调用的方式
  • 修改Spatial-MLLM项目,使其专注于无人机航拍视频的空间理解
  • Electron 应用中的内容安全策略 (CSP) 全面指南
  • AbMole| H₂DCFDA(M9096;活性氧(ROS)探针)
  • 医学编码:临床试验数据标准化的关键
  • Next.js 安装使用教程
  • 多容器应用与编排——AI教你学Docker
  • Web性能测试常用指标(转自百度AI)
  • 开关电源和线性电源Multisim电路仿真实验汇总——硬件工程师笔记
  • 暖通锅炉的智能管控:物联网实现节能又舒适​
  • grom使用mysql快速上手
  • [论文阅读] 人工智能 + 软件工程 | 从软件工程视角看大语言模型:挑战与未来之路
  • 使用 icinga2 写入 TDengine
  • 基于ApachePOI实现百度POI分类快速导入PostgreSQL数据库实战
  • SpringBoot计时一次请求耗时
  • 基于netmiko模块实现支持SSH or Telnet的多线程多厂商网络设备自动化巡检脚本
  • 浏览器F12开发者工具的使用
  • [Python] -基础篇7-新手常见Python语法错误及解决方案
  • Qt时间显示按钮功能详解
  • openlayers根据图层名称判断图层是否在视口内
  • js代码09
  • Maven安装使用教程
  • java web2(黑马)
  • 阿里云-云效自动部署spring boot项目
  • vue + element-ui实现可拖拽表格