Home

closedtype

Closedtype is a term used in programming language theory to describe a type whose set of values is fixed by its declaration and cannot be extended by external modules. It is often described as a closed algebraic data type or a sealed type. The defining property is that all constructors and operations for the type are determined at compile time, so new cases or variants cannot be introduced outside the defining scope.

In functional languages with algebraic data types, a datatype declaration yields a closed type: clients can

Examples help illustrate the idea. Haskell and ML commonly define closed types via data or datatype declarations

Applications of closedtypes include ensuring exhaustiveness in pattern matching, enabling optimizations, and facilitating modular design and

use
the
provided
constructors,
but
cannot
add
new
ones
without
editing
the
type
definition.
This
makes
pattern
matching
on
values
of
the
type
exhaustively
checkable
by
the
compiler.
In
languages
that
support
sealed
or
closed
hierarchies,
such
as
Java
with
sealed
classes,
the
set
of
allowed
subtypes
is
restricted
to
a
known,
finite
list,
providing
similar
guarantees
about
openness
and
extensibility.
(for
instance,
Color
=
Red
|
Green
|
Blue).
In
object-oriented
contexts,
a
sealed
class
or
interface
constrains
subclassing
to
a
predetermined
set
of
implementations.
By
contrast,
open
types
or
extensible
variants
permit
new
cases
to
be
added
outside
the
original
scope,
often
complicating
safety
guarantees.
security
by
preventing
external
code
from
extending
critical
types.
See
also
sealed
classes,
algebraic
data
types,
abstract
data
types,
and
opaque
types.