Class Hierarchies As Tagged Unions
In a typical class hierarchy in object-oriented programming, each subclass can encapsulate data unique to that class. The metadata used to perform virtual method lookup (for example, the object's vtable pointer in most C++ implementations) identifies the subclass and so effectively acts as a tag identifying the particular data stored by the instance (see RTTI). An object's constructor sets this tag, and it remains constant throughout the object's lifetime.
Nevertheless, a class hierarchy involves true subtype polymorphism; it can be extended by creating further subclasses of the same base type, which could not be handled correctly under a tag/dispatch model. Hence, it is usually not possible to do case analysis or dispatch on a subobject's 'tag' as one would for tagged unions. Some languages such as Scala allow base classes to be "sealed", and unify tagged unions with sealed base classes.
Read more about this topic: Tagged Union
Famous quotes containing the words class and/or unions:
“It is the mark of an educated man to look for precision in each class of things just so far as the nature of the subject admits; it is evidently equally foolish to accept probable reasoning from a mathematician and to demand from a rhetorician demonstrative proofs.”
—Aristotle (384323 B.C.)
“The newly-formed clothing unions are ready to welcome her; but woman shrinks back from organization, Heaven knows why! It is perhaps because in organization one find the truest freedom, and woman has been a slave too long to know what freedom means.”
—Katharine Pearson Woods (18531923)