在编译过程中是专用的泛型还是仅仅用于编译时检查的Java泛型? [英] Are generics specialized during compilation or they are just like java generics only for compile time checks?

查看:184
本文介绍了在编译过程中是专用的泛型还是仅仅用于编译时检查的Java泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现泛型的方法有三种:


  1. 只是一个编译时检查的工具,但是每个模板实例
    被编译为相同的字节/汇编代码实现(Java,如注释type erasure实现中所述)。

  2. 代码(C ++,C#)
  3. #1和#2的组合

在Swift中实现哪一个?

解决方案

Swift首先编译一个实现动态类型检查,但优化器可以选择克隆特定类型的专用实现,当速度与代码大小权衡有意义时。理想情况下,这可以获得始终克隆速度的90%,而不会增加代码大小和编译时间。


There are three ways to implement generics:

  1. Just a tool for compile time checks, but every template instance is compiled to the same byte/assembly code implementation (Java, as noted in comments "type erasure" implementation)

  2. Each template instantiation is compiled to specialized code (C++, C#)

  3. Combination of #1 and #2

Which one is implemented in Swift?

解决方案

Swift starts by compiling a single implementation that does dynamic type checking, but the optimizer can then choose to clone off specialized implementations for particular types when the speed vs code size tradeoffs make sense. Ideally, this gets 90% of the speedup of always cloning, without the code size and compilation time exploding.

这篇关于在编译过程中是专用的泛型还是仅仅用于编译时检查的Java泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆