Skip to content

Type relation

Type relation是programming language的type system的一个非常重要的内容,programming language designer需要进行精准定义;

Subtype relation

subtype是最最典型的type relation,它是OOP中的核心话题,OOP type之间的relation是相对简单的,inheritance则对应了subtype/subclass。与subtype relation相关的词语有:

1) generic and specific/special

在wikipedia Covariance and contravariance (computer science)中:

covariant if it preserves the ordering of types (≤), which orders types from more specific to more generic;

在thegreenplace A polyglot's guide to multiple dispatch中:

Note how the intersections get dispatched to specialized functions when these exist and to a generic catch-all Shape x Shape handler when there is no specialized function.

2) wide and narrow

在wikipedia Covariant return type 中:

More specifically, covariant (wide to narrower) or contravariant (narrow to wider) return type......

关于由type constructor构造的complex type之间的subtype relation规则,在./Covariance-and-contravariance中进行了描述。

Type ordering

可以基于type relation对type进行ordering,现代programming language的很多特性的实现都是建立在type ordering的基础之上的:

1) polymorphism dispatch

现代programming language的dispatch是建立在type ordering的基础上的。

于此相关的有:

  • C++ overload resolution,参见C++\Language-reference\Functions\Function-overload\Overload-resolution章节
  • Subtyping polymorphism,参见Theory\Programming-paradigm\Abstraction-and-polymorphism\Polymorphism章节

NOTE: 只有ordering之后,才能够基于type进行computation,参见Relation-structure-computation\Make-it-computational

Type hierarchy

基于subtype relation可以构建type hierarchy。