Home

sumptype

Sumptype is a type construction used in programming languages and type theory to represent a value that may be one of several distinct types. It is commonly called a sum type or tagged union, emphasizing its additive nature: the type is a disjoint union of its variants, each with its own payload.

Construction and syntax often involve declaring multiple variants, each carrying its own data. In pseudocode, a

Semantics and usage revolve around pattern matching or case analysis. Programs typically inspect the tag or

Relation to other concepts: sumptype is the dual of product types and closely mirrors coproducts in category

sumptype
can
be
written
as
a
type
with
several
constructors,
for
example
a
variant
carrying
an
integer
or
a
string.
In
Haskell,
a
similar
concept
appears
as
data
SumType
=
IntCase
Int
|
StringCase
String.
In
OCaml,
a
corresponding
form
is
type
sumtype
=
IntCase
of
int
|
StringCase
of
string.
In
TypeScript,
a
practical
representation
uses
discriminated
unions,
for
instance
type
SumType
=
{
tag:
"IntCase",
value:
number
}
|
{
tag:
"StringCase",
value:
string
}.
These
constructions
allow
values
to
be
created
with
different
shapes
while
remaining
within
a
single,
composable
type.
variant
to
determine
how
to
operate
on
the
contained
payload,
with
exhaustive
matching
ensuring
all
variants
are
handled.
Sumptypes
are
widely
used
for
error
handling
(as
in
Result
or
Either
types),
optional
values,
and
abstract
syntax
trees,
where
different
node
kinds
must
be
combined
within
a
uniform
type.
theory.
It
is
a
foundational
concept
in
algebraic
data
types
and
discriminated
or
variant
unions
in
modern
programming
languages.
See
also
algebraic
data
type,
tagged
union,
coproduct,
pattern
matching.