Polymorphism
“polymorphism”即“多态”,初次见到这个词语,我的第一印象是“多种形态”。
wikipedia Polymorphism (computer science)
In programming languages and type theory, polymorphism is the provision(提供) of a single interface to entities of different types or the use of a single symbol to represent multiple different types.
NOTE: 这段话的意思是:在编程语言和类型理论中,多态性是向不同type的entity提供单一interface,或使用单一符号表示多个不同type。这段话总结了polymorphism的本质,在后面的《Polymorphism的本质》段会进行详细分析。
从上面这段话中,我们可以看到,polymorphism 和 type ,或者更加具体来说 和 type systems 密切相关,参见原文的 History 章节。
The most commonly recognized major classes of polymorphism are:
说明 | 实现方式 | paradigm | ||
---|---|---|---|---|
Ad hoc polymorphism | defines a common interface for an arbitrary set of individually specified types. | - Function overloading - Operator overloading |
||
Parametric polymorphism | when one or more types are not specified by name but by abstract symbols that can represent any type. | - Generic function - Generic programming |
GP、FP | behavior-based |
Subtyping | (also called subtype polymorphism or inclusion polymorphism) when a name denotes instances of many different classes related by some common superclass.[3] |
- Virtual function - Single and dynamic dispatch - Double dispatch - Multiple dispatch |
OOP | inheritance-based |
NOTE:
"ad hoc"的中文意思"特别的"。
NOTE: 上述三者是major classes,其实还有一些其他类型的polymorphism,比如在原文See also中提及的:
Duck typing for polymorphism without (static) types,在后面的"Duck typing for polymorphism without (static) types"章节进行了具体介绍。
在原文的Types段,描述了更多类型的polymorphism。
Types
NOTE: 本段描述各种类型的polymorphism。
Ad hoc polymorphism
NOTE: 特别多态
Main article: Ad hoc polymorphism
The term "ad hoc" in this context is not intended to be pejorative(轻蔑词); it refers simply to the fact that this type of polymorphism is not a fundamental feature of the type system.
Implicit type conversion has also been defined as a form of polymorphism, referred to as "coercion(强制) polymorphism".[2][6]
Parametric polymorphism
NOTE: 参数多态性
Main article: Parametric polymorphism
Parametric polymorphism allows a function or a data type to be written generically, so that it can handle values uniformly without depending on their type.[7] Parametric polymorphism is a way to make a language more expressive while still maintaining full static type-safety.
NOTE: 在Generic programming、Functional programming中,都涉及parametric polymorphism。
The concept of parametric polymorphism applies to both data types and functions.
1) A function that can evaluate to or be applied to values of different types is known as a polymorphic function.
2) A data type that can appear to be of a generalized type (e.g. a list with elements of arbitrary type) is designated polymorphic data type like the generalized type from which such specializations are made.
Parametric polymorphism is ubiquitous(普遍存在) in functional programming, where it is often simply referred to as "polymorphism". The following example in Haskell shows a parameterized list data type and two parametrically polymorphic functions on them:
Subtyping
Main article: Subtyping
NOTE: 在OOP中,普遍存在;由于subtyping涉及很多OOP的内容,将subtyping相关的内容放到了
Theory\Programming-paradigm\Object-oriented-programming\Subtyping-polymorphism
章节中
Object-oriented programming languages offer subtype polymorphism using subclassing (also known as inheritance).
NOTE: subclassing 和 inheritance 可以密切相关但是又有所不同,这在
Theory\Programming-paradigm\Object-oriented-programming\Subtyping-polymorphism
章节进行了介绍。
In typical implementations, each class contains what is called a virtual table—a table of functions that implement the polymorphic part of the class interface—and each object contains a pointer to the "vtable" of its class, which is then consulted whenever a polymorphic method is called. This mechanism is an example of:
- late binding, because virtual function calls are not bound until the time of invocation;
- single dispatch (i.e. single-argument polymorphism), because virtual function calls are bound simply by looking through the vtable provided by the first argument (the
this
object), so the runtime types of the other arguments are completely irrelevant.
NOTE:
这完全是以C++来描述的。
上面从实现层面描述了仅仅支持single dispatch的原因,是非常具备启发性的。
The same goes for most other popular object systems.
NOTE: 这段话的意思是: 大多数programming language都是按照这个思路来实现的。下面列举了一些反例:
Some, however, such as Common Lisp Object System, provide multiple dispatch, under which method calls are polymorphic in all arguments.
NOTE: 从上面的描述,可以看出single dispatch、multiple dispatch之间的差异。
The interaction(相互作用,相互影响) between parametric polymorphism and subtyping leads to the concepts of variance and bounded quantification.
NOTE: "variance"即"可变性",在
Theory\Type-system\Type-constructor
章节进行了描述。bounded quantification在
Programming-paradigm\Generic-programming\Generics
章节进行了描述。
Row polymorphism
NOTE: “row”即“行”
Main article: Row polymorphism
See also: Duck typing
Duck typing for polymorphism without (static) types
NOTE: 这是原文放在See also段中的一段话,我将它移到了这里,因为duck typing也是一种polymorphsim,并且python就是采用的duck typing。Duck typing for polymorphism 更加类似于 Parametric polymorphism,都是:
"when one or more types are not specified by name but by abstract symbols that can represent any type."
下面是关于duck typing和polymorphism的一些补充。
Implementation aspects
NOTE: 参见
Theory\Programming-paradigm\Polymorphism\Implementation
章节
Polymorphism的本质
Polymorphism: dispatch to concrete/implementation automatically
下面是我polymorphism的本质的思考:
1) 描述了一种one-to-many关系:
polymorphism中的one-to-many关系的含义是多重的:
- single(one) interface to entities(many) of different types
- a single(one) symbol to represent multiple(many) different types
- "一个抽象(one),可能有多个(many)实现"(参见文章《Abstraction》)
2) 按照one-to-many关系, 自动实现**map/dispatch**:
这种**map/dispatch**是由**programming language的实现**来提供的(或者说: 这种**map/dispatch**是programming language内置的dispatch机制):
programming language的实现**按照one-to-many关系来实现**map/dispatch,这就是标题中的**自动**的含义。显然,Polymorphism让我们不用写一堆的**if-else**判断。
Polymorphism is a generic trait
本节标题的含义是:多态性是一种通用特性,它源自于Is duck typing a subset of polymorphism的回答:
I say that polymorphism is a generic trait, that can be implemented several ways:
- class based inheritance.
- prototype based objects (with or without inheritance)
- duck typing
- interface compliance (as done by Go's interfaces and implicitly on C++ templates)
each of them allows the programmer to use a single code with different types, so all embody the concept of polymorphism.
Polymorphism的价值
1) 让programmer写出更加abstract的code,能够增强code reuse
2) 使代码更加地generic(参见generic programming),从而带来更好的扩展性
3) 降低编写成本,使我们无需写一堆的if-else
if-else
其实也是描述的one-to-many,使用polymorphism能够让我们运用programming language内置的dispatch机制,避免显式地使用if-else
。
相比于显式地使用if-else
,polymorphism有如下优势:
- 更加高效
- 代码维护性更高、复杂度更低(可以使用**圈复杂度**来进行量化)