Category Polymorphism

Click on the above link to see a complete list of pages in this category.

Poly Morphism (what a horrible Wiki Name, but what can you do?) is the development of algorithms or data structures (or more concrete language constructs, such as objects or functions) which operate successfully on multiple types, doing things appropriate for the type.

Some take issue with polymorphism being based solely on "types". Answering a particular message (method call) does not necessarily depend on typing. Your viewpoint probably depends on where you stand in the static-versus-dynamic Holy War.

[Note that even when there isn't a formal type system per se, one can still divine the concept of type. Smalltalk is often called untyped. However, if you have a set of classes that respond to the message

doThis:
andThat: andTheOther:

one could say that this set represents a type, distinct from its inverse (which doesn't respond to the above message). Of course, you are getting away from type hierarchies, and into set-based or predicate-based type systems. Which are more flexible than hierarchies, but arguably more difficult to implement efficiently. -- Scott Johnson]

I am not sure set-based types or predicate-based types could even be called "types". I suppose one's world view could be that Everything Isa Type, for good or bad. But, that is a subjective perception, not necessarily something objectively in say Smalltalk.


Types of polymorphism:

Generic Polymorphism, also known as Parametric Polymorphism:
Generic algorithms, templates, parameteric types, etc. See Generic Functions (for Lisp discussion), Category Cpp Templates (they get a category all their own! Lots of useful links in here).

Subtype Polymorphism:
Polymorphism based on subtyping (the notion that one type can extend/implement the behavior of another in such fashion that any valid operation on the supertype is valid on the subtype), subsumption (allowing a subtype to be used in a context where a supertype is expected), and Dynamic Dispatch (selecting a function or data based on the runtime type of its arguments - see Single Dispatch, Double Dispatch, Multiple Dispatch (aka Multi Methods)). Java Language, Cee Plus Plus, etc., all have subtype polymorphism. See also Virtual Functions, Dynamic Cast, Run Time Type Information

Boxing Conversions:
Automagically converting expressions of a type which has undergone Type Erasure to one that hasn't or vice versa. Going from "int" (an intrinsic type) to "Integer" (a class) in Java 1.5 (and Csharp Language, though the names might be different) is one example.

Ad Hoc Polymorphism:
Anything else.

Issues in polymorphism:

Internal Polymorphism vs External Polymorphism. Does a type itself "contain" knowledge of its identity, and participate in the implementation of polymorphism? Or is it entirely external to the type definition?

Pointer Cast Polymorphism. What to do if you are working in a low-level language (like Cee Language)

Inheritance vs delegation vs redirection. Inheritance (subclassing) is when all parts of a base type object are pasted into a derived type object (with some of them capable of being overwritten). Delegation (Delegation Pattern) is kindasorta "inheritance by pointer", where a reference to a base type object is stored in the derived object; however, the subtype relationship is preserved. (Sometimes, multiple instances of a derived object can share the same base object instance). Redirection is similar to delegation, but it's "one-way". Polymorphism And Inheritance, Delegation Inheritance, Delegation Is Inheritance, Prototype Based Programming, Use Composition And Interfaces Without Class Inheritance, Composition Instead Of Inheritance

A few well-known modeling prolems. Circle And Ellipse Problem (see also Circles Aint Ellipses, Circles Are Ellipses)

Some argue that subtyping (and/or types themselves) is flawed, disagree on how subtypes ought to act with regards to their supertypes, a Useful Lie, or perhaps a not-so-useful lie. See Polymorphism Limits, Context Sensitive Subtyping, Limits Of Hierarchies, Liskov Substitution Principle, What Are Types, There Are No Types, Sets Versus Types

Are there other better ways of implementing polymorhpism than the ones we've seen implemented so far?



See original on c2.com