Skip to content

Commit

Permalink
feat(basic): add generic
Browse files Browse the repository at this point in the history
  • Loading branch information
shgopher committed Jan 30, 2024
1 parent e332cf7 commit e8cafca
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 基础/泛型/README.md
Original file line number Diff line number Diff line change
@@ -339,6 +339,18 @@ func AbsDifference[T NumericAbs[T]](a, b T) T {
- c 语言:本身不具有泛型,需要程序员去实现一个泛型,实现复杂,但是不增加语言的复杂度 (换言之只增加了程序员的)
- c++和 rust:跟 go 基本保持一种方式,就是增加编译器的工作量
- Java:将泛型装箱为 object,在装箱和拆箱擦除类型的过程中,程序执行效率会变低
### 为什么 Java 编译器不在编译期就完成泛型的装箱操作呢?
主要有以下几个原因:
1. 编译期装箱需要编译器对代码细节有完整信息。但是泛型类型信息可能来自其他模块或第三方库,编译器难以完整获取。
2. 即使编译器可以提前装箱,也需要代码里有大量反射样板代码来还原原始泛型类型,违背泛型设计初衷。
3. 编译期装箱也无法很好处理运行期检查和转换的各种边界情况。
## 用例子学泛型
理论学习完了,不使用例子进行复习的话会忘的很快的。跟着我看几个例子吧

0 comments on commit e8cafca

Please sign in to comment.