C3 Linearization
   HOME

TheInfoList



OR:

"In
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of p ...
systems with multiple inheritance, some mechanism must be used for resolving conflicts when inheriting different definitions of the same property from multiple superclasses." C3 superclass linearization is an
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
used primarily to obtain the order in which
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
should be inherited in the presence of
multiple inheritance Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
. In other words, the ''output'' of C3 superclass linearization is a deterministic Method Resolution Order (MRO). C3 superclass linearization is called C3 because it "is consistent with three properties": * a consistent extended precedence graph, * preservation of local precedence order, and * fitting a monotonicity criterion. It was first published at the 1996
OOPSLA OOPSLA (Object-Oriented Programming, Systems, Languages & Applications) is an annual ACM research conference. OOPSLA mainly takes place in the United States, while the sister conference of OOPSLA, ECOOP, is typically held in Europe. It is opera ...
conference, in a paper entitled "A Monotonic Superclass Linearization for Dylan". It was adapted to the Open Dylan implementation in January 2012 following an enhancement proposal. It has been chosen as the default algorithm for method resolution in
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
2.3 (and newer), Raku,
Parrot Parrots, also known as psittacines (), are birds of the roughly 398 species in 92 genera comprising the order Psittaciformes (), found mostly in tropical and subtropical regions. The order is subdivided into three superfamilies: the Psittacoide ...
,
Solidity Solidity is an object-oriented programming language for implementing smart contracts on various blockchain platforms, most notably, Ethereum. It was developed by Christian Reitwiessner, Alex Beregszaszi, and several former Ethereum core cont ...
, and
PGF/TikZ PGF/Ti''k''Z is a pair of languages for producing vector graphics (e.g., technical illustrations and drawings) from a geometric/algebraic description, with standard features including the drawing of points, lines, arrows, paths, circles, ellipse ...
's Object-Oriented Programming module. It is also available as an alternative, non-default MRO in the core of
Perl 5 Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
starting with version 5.10.0. An extension implementation for earlier versions of Perl 5 named Class::C3 exists on
CPAN The Comprehensive Perl Archive Network (CPAN) is a repository of over 250,000 software modules and accompanying documentation for 39,000 distributions, written in the Perl programming language by over 12,000 contributors. ''CPAN'' can denote eit ...
. Python's
Guido van Rossum Guido van Rossum (; born 31 January 1956) is a Dutch programmer best known as the creator of the Python programming language, for which he was the "benevolent dictator for life" (BDFL) until he stepped down from the position on 12 July 2018 ...
summarizes C3 superclass linearization thus:


Description

The C3 superclass linearization of a class is the sum of the class plus a unique merge of the linearizations of its parents and a list of the parents itself. The list of parents as the last argument to the merge process preserves the local precedence order of direct parent classes. The merge of parents' linearizations and parents list is done by selecting the first head of the lists which does not appear in the tail (all elements of a list except the first) of any of the lists. Note, that a good head may appear as the first element in multiple lists at the same time, but it is forbidden to appear anywhere else. The selected element is removed from all the lists where it appears as a head and appended to the output list. The process of selecting and removing a good head to extend the output list is repeated until all remaining lists are exhausted. If at some point no good head can be selected, because the heads of all remaining lists appear in any one tail of the lists, then the merge is impossible to compute due to inconsistent orderings of dependencies in the inheritance hierarchy and no linearization of the original class exists. A naive divide-and-conquer approach to computing the linearization of a class may invoke the algorithm recursively to find the linearizations of parent classes for the merge-subroutine. However, this will result in an infinitely looping recursion in the presence of a cyclic class hierarchy. To detect such a cycle and to break the infinite recursion (and to reuse the results of previous computations as an optimization), the recursive invocation should be shielded against re-entrance of a previous argument by means of a cache or memoization. This algorithm is similar to finding a
topological ordering In computer science, a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge ''uv'' from vertex ''u'' to vertex ''v'', ''u'' comes before ''v'' in the ordering. For in ...
.


Example

Given the linearization of Z is computed as


Example demonstrated in Python 3

First, a metaclass to enable a short representation of the objects by name instead of, for example, : class Type(type): def __repr__(cls): return cls.__name__ class O(object, metaclass=Type): pass Next, we define our base classes: class A(O): pass class B(O): pass class C(O): pass class D(O): pass class E(O): pass Then we construct the inheritance tree: class K1(C, A, B): pass class K3(A, D): pass class K2(B, D, E): pass class Z(K1, K3, K2): pass And now: >>> Z.mro() , K1, C, K3, A, K2, B, D, E, O,


Example demonstrated in Raku

Raku uses C3 linearization for classes by default: class A class B class C class D class E class K1 is C is A is B class K3 is A is D class K2 is B is D is E class Z is K1 is K3 is K2 say Z.^mro; # OUTPUT: ((Z) (K1) (C) (K3) (A) (K2) (B) (D) (E) (Any) (Mu)) (the Any and Mu are the types all Raku objects inherit from)


References

{{DEFAULTSORT:C3 Linearization Object-oriented programming Programming language implementation Articles with example Python (programming language) code