<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
tagged	B-Language
union	I-Language
,	O
also	O
called	O
a	O
variant	O
,	O
variant	B-Language
record	I-Language
,	O
choice	O
type	O
,	O
discriminated	O
union	O
,	O
disjoint	O
union	O
,	O
sum	B-Language
type	I-Language
or	O
coproduct	O
,	O
is	O
a	O
data	B-General_Concept
structure	I-General_Concept
used	O
to	O
hold	O
a	O
value	O
that	O
could	O
take	O
on	O
several	O
different	O
,	O
but	O
fixed	O
,	O
types	O
.	O
</s>
<s>
Like	O
ordinary	O
unions	O
,	O
tagged	B-Language
unions	I-Language
can	O
save	O
storage	O
by	O
overlapping	O
storage	O
areas	O
for	O
each	O
type	O
,	O
since	O
only	O
one	O
is	O
in	O
use	O
at	O
a	O
time	O
.	O
</s>
<s>
Tagged	B-Language
unions	I-Language
are	O
most	O
important	O
in	O
functional	B-Language
languages	I-Language
such	O
as	O
ML	B-Language
and	O
Haskell	B-Language
,	O
where	O
they	O
are	O
called	O
datatypes	O
(	O
see	O
algebraic	O
data	O
type	O
)	O
and	O
the	O
compiler	O
is	O
able	O
to	O
verify	O
that	O
all	O
cases	O
of	O
a	O
tagged	B-Language
union	I-Language
are	O
always	O
handled	O
,	O
avoiding	O
many	O
types	O
of	O
errors	O
.	O
</s>
<s>
Tagged	B-Language
unions	I-Language
are	O
often	O
accompanied	O
by	O
the	O
concept	O
of	O
a	O
type	O
constructor	O
,	O
which	O
is	O
similar	O
but	O
not	O
the	O
same	O
as	O
a	O
constructor	O
for	O
a	O
class	O
.	O
</s>
<s>
Type	O
constructors	O
produce	O
a	O
tagged	B-Language
union	I-Language
type	O
,	O
given	O
the	O
initial	O
tag	O
type	O
and	O
the	O
corresponding	O
type	O
.	O
</s>
<s>
Mathematically	O
,	O
tagged	B-Language
unions	I-Language
correspond	O
to	O
disjoint	O
or	O
discriminated	O
unions	O
,	O
usually	O
written	O
using	O
+	O
.	O
</s>
<s>
In	O
type	O
theory	O
,	O
a	O
tagged	B-Language
union	I-Language
is	O
called	O
a	O
sum	B-Language
type	I-Language
.	O
</s>
<s>
Sum	B-Language
types	I-Language
are	O
the	O
dual	O
of	O
product	O
types	O
.	O
</s>
<s>
The	O
sum	B-Language
type	I-Language
corresponds	O
to	O
intuitionistic	O
logical	O
disjunction	O
under	O
the	O
Curry	O
–	O
Howard	O
correspondence	O
.	O
</s>
<s>
An	O
enumerated	B-Language
type	I-Language
can	O
be	O
seen	O
as	O
a	O
degenerate	O
case	O
:	O
a	O
tagged	B-Language
union	I-Language
of	O
unit	O
types	O
.	O
</s>
<s>
Many	O
programming	O
techniques	O
and	O
data	B-General_Concept
structures	I-General_Concept
,	O
including	O
rope	B-Data_Structure
,	O
lazy	O
evaluation	O
,	O
class	B-Application
hierarchy	I-Application
(	O
see	O
below	O
)	O
,	O
arbitrary-precision	B-Algorithm
arithmetic	I-Algorithm
,	O
CDR	B-Language
coding	I-Language
,	O
the	O
indirection	O
bit	O
and	O
other	O
kinds	O
of	O
tagged	O
pointers	O
,	O
etc	O
.	O
</s>
<s>
are	O
usually	O
implemented	O
using	O
some	O
sort	O
of	O
tagged	B-Language
union	I-Language
.	O
</s>
<s>
A	O
tagged	B-Language
union	I-Language
can	O
be	O
seen	O
as	O
the	O
simplest	O
kind	O
of	O
self-describing	O
data	B-Application
format	I-Application
.	O
</s>
<s>
The	O
tag	O
of	O
the	O
tagged	B-Language
union	I-Language
can	O
be	O
seen	O
as	O
the	O
simplest	O
kind	O
of	O
metadata	O
.	O
</s>
<s>
The	O
primary	O
advantage	O
of	O
a	O
tagged	B-Language
union	I-Language
over	O
an	O
untagged	O
union	O
is	O
that	O
all	O
accesses	O
are	O
safe	O
,	O
and	O
the	O
compiler	O
can	O
even	O
check	O
that	O
all	O
cases	O
are	O
handled	O
.	O
</s>
<s>
The	O
primary	O
advantage	O
of	O
a	O
tagged	B-Language
union	I-Language
over	O
a	O
simple	O
record	O
containing	O
a	O
field	O
for	O
each	O
type	O
is	O
that	O
it	O
saves	O
storage	O
by	O
overlapping	O
storage	O
for	O
all	O
the	O
types	O
.	O
</s>
<s>
Some	O
implementations	O
reserve	O
enough	O
storage	O
for	O
the	O
largest	O
type	O
,	O
while	O
others	O
dynamically	O
adjust	O
the	O
size	O
of	O
a	O
tagged	B-Language
union	I-Language
value	O
as	O
needed	O
.	O
</s>
<s>
When	O
the	O
value	O
is	O
immutable	B-Application
,	O
it	O
is	O
simple	O
to	O
allocate	O
just	O
as	O
much	O
storage	O
as	O
is	O
needed	O
.	O
</s>
<s>
The	O
main	O
disadvantage	O
of	O
tagged	B-Language
unions	I-Language
is	O
that	O
the	O
tag	O
occupies	O
space	O
.	O
</s>
<s>
Common	O
examples	O
of	O
this	O
are	O
the	O
use	O
of	O
reserved	O
values	O
,	O
where	O
,	O
for	O
example	O
,	O
a	O
function	O
returning	O
a	O
positive	O
number	O
may	O
return	O
-1	O
to	O
indicate	O
failure	O
,	O
and	O
sentinel	B-Data_Structure
values	I-Data_Structure
,	O
most	O
often	O
used	O
in	O
tagged	O
pointers	O
.	O
</s>
<s>
Sometimes	O
,	O
untagged	O
unions	O
are	O
used	O
to	O
perform	O
bit-level	O
conversions	O
between	O
types	O
,	O
called	O
reinterpret	O
casts	O
in	O
C++	B-Language
.	O
</s>
<s>
Tagged	B-Language
unions	I-Language
are	O
not	O
intended	O
for	O
this	O
purpose	O
;	O
typically	O
a	O
new	O
value	O
is	O
assigned	O
whenever	O
the	O
tag	O
is	O
changed	O
.	O
</s>
<s>
While	O
universal	O
data	O
types	O
are	O
comparable	O
to	O
tagged	B-Language
unions	I-Language
in	O
their	O
formal	O
definition	O
,	O
typical	O
tagged	B-Language
unions	I-Language
include	O
a	O
relatively	O
small	O
number	O
of	O
cases	O
,	O
and	O
these	O
cases	O
form	O
different	O
ways	O
of	O
expressing	O
a	O
single	O
coherent	O
concept	O
,	O
such	O
as	O
a	O
data	B-General_Concept
structure	I-General_Concept
node	O
or	O
instruction	O
.	O
</s>
<s>
Also	O
,	O
there	O
is	O
an	O
expectation	O
that	O
every	O
possible	O
case	O
of	O
a	O
tagged	B-Language
union	I-Language
will	O
be	O
dealt	O
with	O
when	O
it	O
is	O
used	O
.	O
</s>
<s>
Like	O
option	O
types	O
and	O
exception	B-General_Concept
handling	I-General_Concept
,	O
tagged	B-Language
unions	I-Language
are	O
sometimes	O
used	O
to	O
handle	O
the	O
occurrence	O
of	O
exceptional	O
results	O
.	O
</s>
<s>
This	O
use	O
of	O
tagged	B-Language
unions	I-Language
can	O
be	O
formalized	O
as	O
a	O
monad	O
with	O
the	O
following	O
functions	O
:	O
</s>
<s>
In	O
ML	B-Language
,	O
we	O
would	O
do	O
this	O
by	O
creating	O
a	O
datatype	O
like	O
this	O
:	O
</s>
<s>
This	O
is	O
a	O
tagged	B-Language
union	I-Language
with	O
two	O
cases	O
:	O
one	O
,	O
the	O
leaf	O
,	O
is	O
used	O
to	O
terminate	O
a	O
path	O
of	O
the	O
tree	O
,	O
and	O
functions	O
much	O
like	O
a	O
null	O
value	O
would	O
in	O
imperative	O
languages	O
.	O
</s>
<s>
In	O
ALGOL	B-Language
68	I-Language
,	O
tagged	B-Language
unions	I-Language
are	O
called	O
united	B-Language
modes	I-Language
,	O
the	O
tag	O
is	O
implicit	O
,	O
and	O
the	O
case	O
construct	O
is	O
used	O
to	O
determine	O
which	O
field	O
is	O
tagged	O
:	O
</s>
<s>
(	O
compl	O
c	B-Language
)	O
:	O
print( ( 	O
"	O
compl	O
:	O
"	O
,	O
c	B-Language
)	O
)	O
,	O
</s>
<s>
Although	O
primarily	O
only	O
functional	B-Language
languages	I-Language
such	O
as	O
ML	B-Language
(	O
from	O
the	O
1970s	O
)	O
and	O
Haskell	B-Language
(	O
from	O
the	O
1990s	O
)	O
give	O
a	O
central	O
role	O
to	O
tagged	B-Language
unions	I-Language
and	O
have	O
the	O
power	O
to	O
check	O
that	O
all	O
cases	O
are	O
handled	O
,	O
other	O
languages	O
have	O
support	O
for	O
tagged	B-Language
unions	I-Language
as	O
well	O
.	O
</s>
<s>
However	O
,	O
in	O
practice	O
they	O
can	O
be	O
less	O
efficient	O
in	O
non-functional	O
languages	O
due	O
to	O
optimizations	O
enabled	O
by	O
functional	B-Language
language	I-Language
compilers	O
that	O
can	O
eliminate	O
explicit	O
tag	O
checks	O
and	O
avoid	B-General_Concept
explicit	I-General_Concept
storage	I-General_Concept
of	I-General_Concept
tags	I-General_Concept
.	O
</s>
<s>
Pascal	B-Application
,	O
Ada	B-Language
,	O
and	O
Modula-2	B-Language
call	O
them	O
variant	B-Language
records	I-Language
(	O
formally	O
discriminated	O
type	O
in	O
Ada	B-Language
)	O
,	O
and	O
require	O
the	O
tag	O
field	O
to	O
be	O
manually	O
created	O
and	O
the	O
tag	O
values	O
specified	O
,	O
as	O
in	O
this	O
Pascal	B-Application
example	O
:	O
</s>
<s>
and	O
this	O
Ada	B-Language
equivalent	O
:	O
</s>
<s>
In	O
C	B-Language
and	O
C++	B-Language
,	O
a	O
tagged	B-Language
union	I-Language
can	O
be	O
created	O
from	O
untagged	O
unions	O
using	O
a	O
strict	O
access	O
discipline	O
where	O
the	O
tag	O
is	O
always	O
checked	O
:	O
</s>
<s>
C	B-Language
and	O
C++	B-Language
also	O
have	O
language	O
support	O
for	O
one	O
particular	O
tagged	B-Language
union	I-Language
:	O
the	O
possibly-null	O
pointer	O
.	O
</s>
<s>
This	O
may	O
be	O
compared	O
to	O
the	O
option	O
type	O
in	O
ML	B-Language
or	O
the	O
Maybe	O
type	O
in	O
Haskell	B-Language
,	O
and	O
can	O
be	O
seen	O
as	O
a	O
tagged	O
pointer	O
:	O
a	O
tagged	B-Language
union	I-Language
(	O
with	O
an	O
encoded	O
tag	O
)	O
of	O
two	O
types	O
:	O
</s>
<s>
Unfortunately	O
,	O
C	B-Language
compilers	O
do	O
not	O
verify	O
that	O
the	O
null	O
case	O
is	O
always	O
handled	O
,	O
and	O
this	O
is	O
a	O
particularly	O
prevalent	O
source	O
of	O
errors	O
in	O
C	B-Language
code	O
,	O
since	O
there	O
is	O
a	O
tendency	O
to	O
ignore	O
exceptional	O
cases	O
.	O
</s>
<s>
One	O
advanced	O
dialect	O
of	O
C	B-Language
called	O
Cyclone	B-Language
has	O
extensive	O
built-in	O
support	O
for	O
tagged	B-Language
unions	I-Language
.	O
</s>
<s>
The	O
enum	B-Language
types	O
in	O
the	O
Rust	B-Application
,	O
Haxe	B-Language
and	O
Swift	B-Application
languages	I-Application
also	O
work	O
as	O
tagged	B-Language
unions	I-Language
.	O
</s>
<s>
The	O
variant	O
library	O
from	O
Boost	B-Language
has	O
demonstrated	O
it	O
was	O
possible	O
to	O
implement	O
a	O
safe	O
tagged	B-Language
union	I-Language
as	O
a	O
library	O
in	O
C++	B-Language
,	O
visitable	O
using	O
function	O
objects	O
.	O
</s>
<s>
Scala	B-Application
has	O
case	O
classes	O
:	O
</s>
<s>
Because	O
the	O
class	B-Application
hierarchy	I-Application
is	O
sealed	O
,	O
the	O
compiler	O
can	O
check	O
that	O
all	O
cases	O
are	O
handled	O
in	O
a	O
pattern	B-Language
match	I-Language
:	O
</s>
<s>
Scala	B-Application
's	O
case	O
classes	O
also	O
permit	O
reuse	O
through	O
subtyping	O
:	O
</s>
<s>
F#	B-Operating_System
has	O
discriminated	O
unions	O
:	O
</s>
<s>
Because	O
the	O
defined	O
cases	O
are	O
exhaustive	O
,	O
the	O
compiler	O
can	O
check	O
that	O
all	O
cases	O
are	O
handled	O
in	O
a	O
pattern	B-Language
match	I-Language
:	O
</s>
<s>
Haxe	B-Language
's	O
enums	B-Language
also	O
work	O
as	O
tagged	B-Language
unions	I-Language
:	O
</s>
<s>
Nim	B-Application
has	O
object	O
variants	O
similar	O
in	O
declaration	O
to	O
those	O
in	O
Pascal	B-Application
and	O
Ada:Macros	O
can	O
be	O
used	O
to	O
emulate	O
pattern	B-Language
matching	I-Language
or	O
to	O
create	O
syntactic	O
sugar	O
for	O
declaring	O
object	O
variants	O
,	O
seen	O
here	O
as	O
implemented	O
by	O
the	O
package	O
:	O
</s>
<s>
Enums	B-Language
are	O
added	O
in	O
Scala	B-Application
3	O
,	O
allowing	O
us	O
to	O
rewrite	O
the	O
earlier	O
Scala	B-Application
examples	O
more	O
concisely	O
:	O
</s>
<s>
The	O
Rust	B-Application
language	I-Application
has	O
extensive	O
support	O
for	O
tagged	B-Language
unions	I-Language
,	O
called	O
enums	B-Language
.	O
</s>
<s>
Rust	B-Application
's	O
error	B-General_Concept
handling	I-General_Concept
model	O
relies	O
extensively	O
on	O
these	O
tagged	B-Language
unions	I-Language
,	O
especially	O
the	O
Optionxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
type	O
,	O
which	O
is	O
either	O
Ok(T )	O
or	O
Err(E )	O
.	O
</s>
<s>
Swift	B-Application
also	O
has	O
substantial	O
support	O
for	O
tagged	B-Language
unions	I-Language
via	O
enumerations	B-Language
.	O
</s>
<s>
For	O
example:With	O
TypeScript	B-Language
it	O
is	O
possible	O
to	O
create	O
tagged	B-Language
unions	I-Language
as	O
well	O
.	O
</s>
<s>
In	O
a	O
typical	O
class	B-Application
hierarchy	I-Application
in	O
object-oriented	B-Language
programming	I-Language
,	O
each	O
subclass	O
can	O
encapsulate	O
data	O
unique	O
to	O
that	O
class	O
.	O
</s>
<s>
The	O
metadata	O
used	O
to	O
perform	O
virtual	B-Application
method	I-Application
lookup	O
(	O
for	O
example	O
,	O
the	O
object	O
's	O
vtable	B-Language
pointer	O
in	O
most	O
C++	B-Language
implementations	O
)	O
identifies	O
the	O
subclass	O
and	O
so	O
effectively	O
acts	O
as	O
a	O
tag	O
identifying	O
the	O
particular	O
data	O
stored	O
by	O
the	O
instance	O
(	O
see	O
RTTI	B-Application
)	O
.	O
</s>
<s>
Nevertheless	O
,	O
a	O
class	B-Application
hierarchy	I-Application
involves	O
true	O
subtype	O
polymorphism	O
;	O
it	O
can	O
be	O
extended	O
by	O
creating	O
further	O
subclasses	O
of	O
the	O
same	O
base	O
type	O
,	O
which	O
could	O
not	O
be	O
handled	O
correctly	O
under	O
a	O
tag/dispatch	O
model	O
.	O
</s>
<s>
Hence	O
,	O
it	O
is	O
usually	O
not	O
possible	O
to	O
do	O
case	O
analysis	O
or	O
dispatch	O
on	O
a	O
subobject	O
's	O
'	O
tag	O
 '	O
as	O
one	O
would	O
for	O
tagged	B-Language
unions	I-Language
.	O
</s>
<s>
Some	O
languages	O
such	O
as	O
Scala	B-Application
allow	O
base	O
classes	O
to	O
be	O
"	O
sealed	O
"	O
,	O
and	O
unify	O
tagged	B-Language
unions	I-Language
with	O
sealed	O
base	O
classes	O
.	O
</s>
