Home

AdhocPolymorphie

AdhocPolymorphie, commonly referred to in English as ad hoc polymorphism, is a form of polymorphism in programming languages in which the behavior of an operation depends on the specific type of its arguments, with separate implementations provided for different types. The dispatch is typically resolved by the compiler or interpreter based on the static type of the operands, rather than by the type of the overall value. It contrasts with parametric polymorphism and with subtype polymorphism, where a single generic implementation or a single method can be used across multiple types.

Common mechanisms include function overloading and operator overloading, where the same function or operator name is

Examples include: overloading of a function like print(x) with separate int, float, and string variants; operator

Advantages of ad hoc polymorphism include expressive syntax and potential performance optimizations from specialized implementations. Drawbacks

associated
with
different
implementations
depending
on
argument
types.
Some
languages
also
provide
ad
hoc
polymorphism
through
type
classes
(or
similar
constructs)
that
select
a
suitable
implementation
based
on
the
type
constraints
present
in
the
program.
In
many
languages,
ad
hoc
polymorphism
can
be
resolved
statically
(at
compile
time)
or
dynamically
(at
run
time),
depending
on
the
language
design.
overloading
such
as
redefining
the
addition
operator
for
a
complex
number
type;
or
a
type
class
that
provides
a
print
method
for
any
type
that
implements
the
interface.
In
contrast,
parametric
polymorphism
would
define
a
single
generic
function
that
operates
on
all
types,
and
subtype
polymorphism
would
dispatch
based
on
an
object's
runtime
class
in
an
inheritance
hierarchy.
can
include
code
duplication,
potential
ambiguity
in
overload
resolution,
and
maintenance
complexity
when
many
types
participate.
It
is
one
of
several
forms
of
polymorphism
used
in
modern
languages,
alongside
parametric
and
subtype
polymorphism.