Bridging Functional and Object-Oriented Programming
Abstract
Proponents of the functional programming paradigm
contend that higher-order functions combined
with (parametric) polymorphism result in
much more reusable code. Object-oriented (OO)
programming has independently followed a parallel
approach to reusability. Objects can be used
instead of higher-order functions and subtype polymorphism
partially substitutes parametric polymorphism.
In this paper, we draw strong analogies between
the object-oriented and functional programming
paradigms. We show that several common OO
design patterns (Visitor, Virtual Proxy, Command,
Observer, and more) are closely related to functional
programming patterns. Additionally, we
show how better support for functional programming
in OO languages can result in improvements
for many design patterns (Command, Virtual
Proxy, Builder, Abstract Factory, and more).
The context for demonstrating our ideas is the
FC++ library. FC++ adds to C++ many of the capabilities
of modern functional programming languages,
without any modiÞcation to the base
language. Compared to other attempts to program
functionally in C++, FC++ offers much richer support
for polymorphic functions (allowing functions
that take polymorphic functions as arguments and/
or return them as results).