This article doesn't cite any references or resources. Please support enhance this informative article by incorporating citations to trustworthy sources. Unsourced substance may possibly be challenged and removed. (August 2009)
In pc science, a tagged union, also called a variant, variant report, discriminated union, or disjoint union, is actually a info framework employed to maintain a worth that may consider on several various,
Tiffany Rings, but fixed kinds. Only one with the kinds can be in use at any 1 time,
Pandora Charms Shop jhgjhg, and a tag area explicitly indicates which one is in use. It may be considered like a type which has numerous "cases," every single of which ought to be handled effectively when that form is manipulated. Like ordinary unions, tagged unions can conserve storage by overlapping storage places for every form, because just one is in use at a time.
Tagged unions are most significant in practical languages these kinds of as ML and Haskell, wherever these are referred to as datatypes (see algebraic info type) and also the compiler has the ability to verify that all situations of the tagged union are usually handled, avoiding numerous varieties of problems. They can, nevertheless, be produced in almost any language, and therefore are much safer than untagged unions, often just called unions, which might be similar but do not explicitly keep track of which member from the union is currently in use.
Tagged unions tend to be accompanied with the notion of the constructor, that's equivalent but not the identical as being a constructor for a class. Constructors produce a tagged union value, offered the original tag value and a worth from the corresponding kind.
Mathematically, tagged unions correspond to disjoint or discriminated unions, usually created using +. Provided an aspect of a disjoint union A + B, it's feasible to ascertain no matter whether it arrived from the or B. If an component lies in equally, there will be two efficiently unique copies from the appeal in a very + B, one from a and a single from B.
In kind concept, a tagged union is called a sum kind. Notations fluctuate, but typically the sum type A + B comes with two introduction varieties and . The elimination type is situation analysis, called pattern matching in ML-style programming languages: if e has sort A + B and e1 and e2 have form τ underneath the assumptions x:A and y:B respectively, then the phrase has sort τ. The sum type corresponds to intuitionistic logical disjunction under the Curry-Howard correspondence.
An enumerated kind could be observed as being a degenerate scenario: a tagged union of unit sorts. It corresponds into a set of nullary constructors and may be carried out as a basic tag variable, considering that it retains no additional info in addition to the worth with the tag.
1 Positive aspects and drawbacks
two Examples
3 Time line of language assistance 3.1 1960s
three.2 1970s & 1980s
3.three 2000s 4 Class hierarchies as tagged unions
5 See also
6 External links [edit] Benefits and disadvantages
The primary advantage of the tagged union over an untagged union is that all accesses are safe, and the compiler can even check that all circumstances are handled. Untagged unions depend on program logic to correctly identify the presently active discipline,
Tiffany Novo, which could result in strange behavior and hard-to-find bugs if that logic fails.
The primary advantage of a tagged union over a straightforward report containing a discipline for every type is that it saves storage by overlapping storage for all the sorts. Some implementations reserve enough storage for the largest kind, while others dynamically adjust the size of the tagged union worth as needed. When the appeal is immutable, it truly is straightforward to allocate just as a lot storage as is needed.
The main disadvantage of tagged unions is that the tag occupies space. Since there are typically a small number of alternatives, the tag can typically be squeezed into two or three bits wherever space can be found, but sometimes even these bits are not available. In this case, a helpful alternative could be folded, computed or encoded tags, wherever the tag value is dynamically computed from the contents of the union discipline. Common examples of this are the use of reserved values, where, for example, a function returning a positive number could return -1 to indicate failure, and sentinel values,
Tiffany Sale, most often employed in tagged pointers.
Sometimes, untagged unions are utilised to perform bit-level conversions between sorts, named reinterpret casts in C++. Tagged unions are not intended for this purpose; typically a new value is assigned whenever the tag is changed.
Many languages support, to some extent, a universal data form, that's a kind that includes every value of every other sort,
Tiffany Ring, and frequently a way is provided to test the actual sort of a worth of the universal form. These are sometimes referred to as variants. While universal info varieties are comparable to tagged unions in their formal definition, typical tagged unions include a relatively small number of situations, and these instances type diverse ways of expressing a single coherent idea, these kinds of like a information framework node or instruction. Also, there is an expectation that every feasible situation of the tagged union is going to be dealt with when it truly is employed. The values of a universal info sort are not related and there is no feasible way to deal with them all.
Like option kinds and exception handling, tagged unions are sometimes employed to handle the occurrence of exceptional results. Typically these tags are folded into the form as "reserved values",
Tiffany Style, and their occurrence is not consistently checked: this can be a fairly common source of programming problems. This use of tagged unions may be formalized like a monad with the following functions:
where "value" and "err" are the constructors of the union type, A and B are valid result kinds and E is the type of error conditions. Alternately, exactly the same monad could be described by return and two added functions, fmap and join:
[edit] Examples
Say we wanted to build a binary tree of integers. In ML, we would do this by creating a datatype like this:
datatype tree = Leaf | Node of (int * tree * tree)
This is really a tagged union with two situations: a single, the leaf, is utilized to terminate a path in the tree, and functions a lot like a null worth would in imperative languages. The other branch retains a node, which contains an integer along with a left and right subtree. Leaf and Node are the constructors, which enable us to actually create a particular tree, these kinds of as:
Node(5, Node(1,Leaf,Leaf), Node(three, Leaf, Node(4, Leaf, Leaf)))
which corresponds to this tree:
Now we can easily write a typesafe function that, say, counts the number of nodes in the tree:
fun countNodes(Leaf) = 0 | countNodes(Node(int,left,right)) = 1 + countNodes(left) + countNodes(right) [edit] Time line of language support
[edit] 1960s
In ALGOL 68, tagged unions are named united modes, the tag is implicit, and also the case construct is utilised to ascertain which area is tagged:
mode node = union (real, int,
Tiffany Earrings, compl, string);
Usage example for union circumstance of node:
node n := "1234"; circumstance n in (real r): print(("real:", r)), (int i): print(("int:", i)), (compl c): print(("compl:", c)), (string s): print(("string:", s)) out print(("?:", n))
esac [edit] 1970s & 1980s
Although primarily only useful languages this kind of as ML and Haskell (from 1990s) give a central role to tagged unions and have the power to check that all cases are handled, other languages have support for tagged unions as well. However, in practice they are able to be less efficient in non-functional languages due to optimizations enabled by functional language compilers that can eliminate explicit tag checks and avoid explicit storage of tags.
Pascal, Ada,
Tiffany Jewellery, and Modula-2 call them variant records (formally discriminated sort in Ada), and require the tag area to be manually created as well as the tag values specified,
Tiffany Charm Bracelet, as in this Pascal example:
And this Ada example:
In C and C++, a tagged union may be created from untagged unions making use of a strict access discipline where the tag is usually checked:
As long as the union fields are only accessed through the functions, the accesses will probably be safe and correct. The identical approach might be used for encoded tags; we just decode the tag and then check it on each and every access. If the inefficiency of these tag checks is actually a concern, they may be automatically removed in the final version.
C and C++ also have language help for one particular tagged union: the possibly-null pointer. This could be compared to the option sort in ML or the Maybe sort in Haskell, and can be seen as being a tagged pointer: a tagged union (with an encoded tag) of two types:
Valid pointers,
A form with just one worth, null, indicating an exceptional condition.
Unfortunately, C compilers do not verify that the null case is always handled, and this is a particularly prevalent source of errors in C code, considering that there is actually a tendency to ignore exceptional instances.
[edit] 2000s
One advanced dialect of C called Cyclone has extensive built-in assistance for tagged unions. See the tagged union section of the on-line manual for more information.
The variant library from Boost has demonstrated it was possible to implement a safe tagged union as being a library in C++, visitable employing functors.
[edit] Class hierarchies as tagged unions
In a typical class hierarchy in object-oriented programming, every single subclass can encapsulate data unique to that class. The metadata utilized 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 could be extended by creating further subclasses in the very same base form, which could not be handled properly under a tag/dispatch model. Hence, it really is usually not possible to do scenario analysis or dispatch on a subobject's 'tag' as one particular would for tagged unions. Some languages these as Scala allow base classes to be "sealed", and unify tagged unions with sealed base classes.
[edit] See also Discriminator v · d · eData varieties Uninterpreted Numeric Text Pointer Composite Other Related topics
[edit] External links boost::variant can be a C++ typesafe discriminated union
std.variant is an implementation of variant form in D 2.0