Ad hoc polymorphism: Difference between revisions

Content deleted Content added
Overloading: Attempt to use inline code for clarity
Overloading: Make some small improvements
Line 28:
 
===Overloading===
To handle these six function calls, four different pieces of code are needed— (or ''three'', if strings are considered to be lists of characters):
* In the first case, [[integer (computer science)|integer]] addition must be invoked.
* In the second and third cases, [[floating point|floating-point]] addition must be invoked (with [[type promotion]], or [[type coercion]], in the third case).
* In the fourth and fifth cases, [[List (computing)|list]] [[concatenation]] must be invoked.
* In the last case, [[literal string|string]] concatenation must be invoked.
Thus, the name <code>+</code> actually refers to three or four completely different functions. This is an example of ''[[Overloading (programming)|overloading]]''.
 
(Note the ambiguity in the string types used in the last case. Consider <code>"123" + "456"</code> in which the programmer might ''naturally'' assume addition rather than concatenation. They may expect <code>"579"</code> instead of <code>"123456"</code>. Overloading can therefore provide different meaning, or semantics, for an operation, as well as differing implementations.)
 
==References==