局部优先级排序 和 单调性 的线性化 方法解析顺序 Method Resolution Order

A monotonic superclass linearization for Dylan | Proceedings of the 11th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications

 

对于多继承层级结构,线性化的构造就比较麻烦了,因为要构造一个尊重 局部优先级排序 和 单调性 的线性化将更为困难

 

Python 2.3 方法解析顺序 — Python 3.15.2 文档

The Python 2.3 Method Resolution Order — Python 3.15.2 documentation

C3线性化_百度百科

C3线性化是多继承编程中确定方法解析顺序的算法,由1996年OOPSLA会议论文《A Monotonic Superclass Linearization for Dylan》首次提出。该算法被Python 2.3选作默认方法解析机制,并应用于Perl 6、Solidity等语言,Perl 5自5.10.0版本起提供可选支持 [1] [3-4]。
其核心原理通过合并父类线性化列表实现,按迭代选择不出现在各列表尾部的首个元素生成输出列表,若无法选元素则判定继承体系存在冲突。Solidity采用反向C3线性化解决钻石继承问题,通过基类排序确定函数调用优先级 [3-4]。Python在2.3版本引入C3算法替代深度优先策略,通过mro属性存储计算后的顺序,使用merge操作合并父类序列并确保子类优先原则。该算法遵循局部优先、单调性和一致性原则,在多重继承中避免方法调用歧义。

 

Object-oriented languages with multiple inheritance and automatic conflict resolution typically use a linearization of superclasses to determine which version of a property to inherit when several superclasses provide definitions. Recent work has defined several desirable characteristics for hnearizations, the most important being monotonicity, which prohibits inherited properties from skipping over direct superclasses. Combined with Dylan’s sealing mechanism, a monotonic linearization enables some compile-time method selection that would otherwise be impossible in the absence of a closed-world assumption.

 

The Dylan linearization is monotonic, easily described, strictly observes local precedence order, and produces the same ordering as CLOS when that is monotonic. We present an implementation based on merging and a survey of class heterarchies from several large programs, analyzing where commonly used linearizations differ. 

 

posted @ 2026-11-20 14:54  papering  阅读(5)  评论(0)    收藏  举报