What's the difference between Polymorphism and Multiple Dispatch?

后端 未结 7 1349
难免孤独
难免孤独 2021-01-30 10:52

...or are they the same thing? I notice that each has its own Wikipedia entry: Polymorphism, Multiple Dispatch, but I\'m having trouble seeing how the concepts differ.

<

相关标签:
7条回答
  • 2021-01-30 11:26

    if you want the conceptual equivalent of a method invocation

    (obj_1, obj_2, ..., obj_n)->method
    

    to depend on each specific type in the tuple, then you want multiple dispatch. Polymorphism corresponds to the case n=1 and is a necessary feature of OOP.

    0 讨论(0)
  • 2021-01-30 11:26

    Multiple Dispatch relies on polymorphism based. Typical polymorphism encountered in C++, C#, VB.NET, etc... uses single dispatch -- i.e. the function that gets called only depends on a single class instance. Multiple dispatch relies on multiple class instances.

    0 讨论(0)
  • 2021-01-30 11:29

    I've never heard of Multiple Dispatch before, but after glancing at the Wikipedia page it looks a lot like MD is a type of polymorphism, when used with the arguments to a method.

    Polymorphism is essentially the concept that an object can be seen as any type that is it's base. So if you have a Car and a Truck, they can both be seen as a Vehicle. This means you can call any Vehicle method for either one.

    Multiple dispatch looks similar, in that it lets you call methods with arguments of multiple types, however I don't see certain requirements in the description. First, it doesn't appear to require a common base type (not that I could imagine implementing THAT without void*) and you can have multiple objects involved.

    So instead of calling the Start() method on every object in a list (which is a classic polymorphism example), you can call a StartObject(Object C) method defined elsewhere and code it to check the argument type at run time and handle it appropriately. The difference here is that the Start() method must be built into the class, while the StartObject() method can be defined outside of the class so the various objects don't need to conform to an interface.

    This could be nice if the Start() method needed to be called with different arguments. Maybe Car.Start(Key carKey) vs. Missile.Start(int launchCode)

    But both could be called as StartObject(theCar) or StartObject(theMissile)

    Interesting concept...

    0 讨论(0)
  • 2021-01-30 11:37

    With multiple dispatch, a method can have multiple arguments passed to it and which implementation is used depends on each argument's type. The order that the types are evaluated depends on the language. In LISP, it checks each type from first to last. Languages with multiple dispatch make use of generic functions, which are just function declarations and aren't like generic methods, which use type parameters.

    Multiple dispatch allows for subtyping polymorphism of arguments for method calls.

    Single dispatch also allows for a more limited kind of polymorphism (using the same method name for objects that implement the same interface or inherit the same base class). It's the classic example of polymorphism, where you have methods that are overridden in subclasses.

    Beyond that, generics provide parametric type polymorphism (i.e., the same generic interface to use with different types, even if they're not related — like List<T>: it can be a list of any type and is used the same way regardless).

    0 讨论(0)
  • 2021-01-30 11:38

    Multiple Dispatch is more akin to function overloading (as seen in Java/C++), except the function invoked depends on the run-time type of the arguments, not their static type.

    0 讨论(0)
  • 2021-01-30 11:46

    Multiple Dispatch is a kind of polymorphism. In Java/C#/C++, there is polymorphism through inheritance and overriding, but that is not multiple dispatch, which is based on two or more arguments (not just this, like in Java/C#/C++)

    0 讨论(0)
提交回复
热议问题