|
Why not elaborate some more? I'm bored, so... 2 + 2 == 4
"Hello" + "World" == "Hello World"
[2, 2] + [3, 4] == [2, 2, 3, 4]
Should we bother reusing `+` for this? Why not, 2 intPlus 2 == 4
"Hello" strPlus "World" == "Hello World"
[2, 2] arrayPlus [3, 4] == [2, 2, 3, 4]
Well: the polymorphism `+` allows us to express a common idea, that of "appending". For each of these specific types: int, string, array we can speak in the application-domain of "appending" whilst in the programming domain of "+"ing if we introduce an interface for `+`, that of "appendable", interface Appendable[A] {
A + A -> A
A + 0 -> A
}
This interface allows us to use `+` generically in a principled way, the technical name for Appendable is `Monoid`, but i prefer Appendable (incidentally, Monad is `Sequencable` or `Nestable`).Polymorphism is just the ability to speak as generically in the programming-domain as we speak in the application domain, ie., to use the double-meanings of ordinary thinking in programming. |