Home

ABtype

ABtype is a theoretical data construct used in programming language theory to represent a value that may be one of two distinct alternatives, A or B. It is a binary sum type, also known as a discriminated union, where exactly one variant is present at runtime.

Formal definition: ABtype<A, B> consists of two constructors, A(value: A) and B(value: B).

Semantics and usage: Values of ABtype are created by one of the two constructors. Pattern matching or

Relation to other types: ABtype is analogous to a sum type found in many languages, such as

Use cases: ABtype is useful for modeling choices between two forms, such as a computation that may

Examples: Haskell-like: data ABtype a b = A a | B b. TypeScript-like: type ABtype<a,b> = { tag: 'A', value:

case
analysis
is
used
to
determine
which
variant
is
active,
and
code
typically
handles
both
possibilities.
The
type
is
parameterizable,
so
ABtype
can
carry
any
pair
of
types
in
A
and
B.
Haskell's
Either
a
b
or
a
TypeScript
discriminated
union.
In
languages
without
native
sum
types,
ABtype
can
be
simulated
with
tagged
objects
or
class
hierarchies
with
an
explicit
tag.
succeed
with
a
value
of
type
A
or
fail
with
a
value
of
type
B,
or
an
input
that
is
either
provided
(A)
or
missing
(B).
It
supports
exhaustive
handling
and
precise
type
safety.
a
}
|
{
tag:
'B',
value:
b
}.