Polymorphic Function

From GM-RKB
Jump to navigation Jump to search

A Polymorphic Function is a software function whose operations can also be applied to values of some other type, or types.



References

2014

  • (Wikipedia, 2014) ⇒ http://en.wikipedia.org/wiki/polymorphism_(computer_science) Retrieved:2014-8-8.
    • In programming languages and type theory, polymorphism (from Greek πολύς, polys, "many, much" and μορφή, morphē, "form, shape") is the provision of a single interface to entities of different types. A polymorphic type is a type whose operations can also be applied to values of some other type, or types. There are several fundamentally different kinds of polymorphism: *If a function denotes different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations, it is called ad hoc polymorphism. Ad hoc polymorphism is supported in many languages using function overloading. *If the code is written without mention of any specific type and thus can be used transparently with any number of new types, it is called parametric polymorphism. In the object-oriented programming community, this is often known as generics or generic programming. In the functional programming community, this is often simply called polymorphism. *Subtyping (or inclusion polymorphism) is a concept wherein a name may denote instances of many different classes as long as they are related by some common superclass.[1] In object-oriented programming, this is often referred to simply as polymorphism.

      The interaction between parametric polymorphism and subtyping leads to the concepts of variance and bounded quantification.

  1. Booch, et all 2007 Object-Oriented Analysis and Design with Applications. Addison-Wesley.

2011

  • (Kinariwala & Dob, 1993) ⇒ Bharat Kinariwala and Tep Dob. (1993). Programming in C. Polymorphic Data Type http://ee.hawaii.edu/~tep/EE160/Book/chap10/section2.1.5.html
    • QUOTE: Very often in programs, a generic operation must be performed on data of different types. For example, in our bubble sort algorithm for the payroll records, when elements were found out of order in the id[] array, we needed to swap the integer elements in that array as well as the float elements in the hrs [] and rate[] arrays. If we decided to implement this swapping operation as a function, we would need to write two functions: one to swap integers, and another to swap floating point values; even though the algorithm for swapping is the same in both cases. (We wrote a swap function for integers using pointers in Chapter).

      The C language provides a mechanism which allows us to write a single swapping function which can be used on any data type. This mechanism is called a polymorphic data type, i.e. a data type which can be transformed to any distinct data type as required. An item of polymorphic data type is created by the use of a generic pointer. A generic pointer is simply a byte address without an associated type. In other words, a generic pointer does not point to an object of a specific type; it just points to some location in the memory of the computer. In ANSI C, a generic pointer is declared as a void pointer (in old C, a generic pointer is a char pointer). It is only when the actual operations must be performed on the data that generic pointers are cast to pointers to specific types and dereferenced.

      Using the concept of a generic pointer, we can assume the following prototype for a function to swap two data items of any type: void gen_swap(void * x, void * y, char type); Here, x and y are generic pointers to two data items, and type specifies the type of the data using a single character