I am learning some polymorphism.
The wiki page for rust addresses trait
is a method to achieve ad hoc polymorphism, and the page for ad hoc polymorphism says function overloading
is an example of ad hoc polymorphism.
Based on my current level of understanding a function is ad hoc polymorphic if providing different types of parameters will invoke different implementations. But trait
and function overloading
seem so different: trait
add constraints on type parameters, any type implementes the trait
is acceptable, while function overloading overload on the concrete type, any types that are not overloaded are not acceptable.
Can I say trait
and function overloading
achieve ad hoc polymorphism in the opposite direction? As trait
is by specialization and overloading
is by generalization?
PS: In c++, template specialization can also provide different implementations based on type parameter passed in, is that also an example of ad hoc polymorphism?