pl-types Computer Science

3 个科学问题 · 计算机 Computer Science

本页目录

两个在不同模块中独立声明、成员完全相同的类,在结构子类型与名义子类型体系下得到的兼容性结论为何不同?各自更适合什么场景?

Two classes declared independently in different modules yet with identical members: why do structural and nominal subtyping disagree on their compatibility, and which situations favor each discipline?

结构子类型只比较类型的形状,成员一致即视为可互换,这两个类因此互相兼容;名义子类型要求显式声明的继承关系,形状相同也不兼容,从而能拦截语义不同却碰巧同形的误配。前者利于跨模块的鸭子式互操作,后者让类型承载设计意图,使编译器在重构时能给出更严格的检查。
Structural subtyping compares shapes, so identical members make the two classes interchangeable; nominal subtyping demands an explicitly declared hierarchy and rejects the match, blocking accidental pairing of types that merely look alike. The structural style eases duck-typed interop across module boundaries, while the nominal style lets types carry design intent and gives the compiler a stricter handle during refactoring.
难度 进阶 · 适合 大众 CC BY 4.0 · 已核验 · Verified 2026-11-02
来源 · Source

B. C. Pierce,《类型与程序设计语言》,MIT 出版社,2002,第 15 章

Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Chapter 15

#类型系统 #子类型

类型擦除使 Java 的 List<String> 与 List<Integer> 在运行时是同一个类,这一设计动机是什么?又带来了哪些语言层面的限制?

Type erasure makes Java's List<String> and List<Integer> the same class at runtime — what motivated the design, and which language-level restrictions follow from it?

擦除是为了让泛型代码与既有 JVM 字节码及泛型出现之前的类库保持二进制兼容,GJ 编译器据此把泛型编译为插入了强制转换的原始类型。代价是运行时无法获知类型实参:不能按 List<String> 重载、不能创建 T 数组、裸类型与可变参数可能引发堆污染,只能靠传递 Class 对象或超类型令牌绕开。
Erasure keeps generic code binary-compatible with existing JVM bytecode and pre-generics libraries; the GJ compiler compiles generics down to raw types with inserted casts. The price is that type arguments vanish at runtime: no overloading on List<String>, no creation of T arrays, heap pollution from raw types and varargs — all worked around by passing Class objects or super type tokens.
难度 进阶 · 适合 初中(12-15) CC BY 4.0 · 已核验 · Verified 2026-11-04
来源 · Source

G. Bracha、M. Odersky、D. Stoutamire 与 P. Wadler,《Making the future safe for the past》,ACM SIGPLAN Notices,1998 (DOI: 10.1145/286942.286957)

G. Bracha, M. Odersky, D. Stoutamire & P. Wadler, 'Making the future safe for the past', ACM SIGPLAN Notices, 1998, DOI: 10.1145/286942.286957

#类型系统 #泛型

Option 类型加上穷尽模式匹配,如何从语言层面消灭空指针一类错误?

How do option types plus exhaustive pattern matching eliminate null-pointer failures at the language level?

可空引用把可能有值这一事实悄悄混进每一个引用类型,编译器对此毫无约束,任何一次解引用都可能踩雷,Hoare 因此称其为十亿美元的错误。Option<T> 把缺席写进类型签名,函数被迫声明自己可能不返回 T,调用方拿到的只会是 Some 或 None 两种构造之一。编译器对 match 做完备性检查,漏掉 None 分支直接报错,整类忘记判空的缺陷从运行期事故变成编译期错误,Rust、Swift、OCaml 均采用此设计。
Nullable references smuggle possible absence into every reference type invisibly, leaving compilers powerless over dereferences—which is precisely why Hoare called null his billion-dollar mistake. An Option<T> makes absence part of the signature: a function declares it may fail to return a T, and callers receive either Some or None, never a bare value. Because the compiler checks match expressions for exhaustiveness, forgetting the None arm becomes a compile-time error, turning an entire class of runtime crashes into rejected code—a design Rust, Swift, and OCaml all share.
难度 进阶 · 适合 大众 CC BY 4.0 · 已核验 · Verified 2026-11-03
来源 · Source

C. A. R. Hoare 2009 年 QCon 演讲《Null References: The Billion Dollar Mistake》;Pierce《Types and Programming Languages》

C. A. R. Hoare, QCon 2009 talk "Null References: The Billion Dollar Mistake"; B. Pierce, "Types and Programming Languages"

#类型系统 #空安全