Simon Peyton Jones
π€ SpeakerAppearances Over Time
Podcast Appearances
So object-oriented programming is another approach to polymorphism.
So in an object-oriented language, we say if we have a Ford that is a car and a car is a vehicle, then anything that works on vehicles should also work on cars and should also work on Fords.
So
The code that we write for cars works unchanged for vehicles and for Fords.
Now, that's a form of polymorphism, not parametric polymorphism.
That's what you might call object-oriented polymorphism or subtype polymorphism.
But polymorphism in the sense that the same code, the same executable code, actually the same machine instructions work on values of different types.
Okay?
In both cases.
Both reverse a list, same machine instructions.
Code that works on vehicles works on things on Ford, same machine instructions, right?
Okay.
That's what polymorphism in general means.
Parametric polymorphism means this full A, list of A's, list of A's stuff.
Subtype polymorphism means if it works on vehicles, it works on any subtype of vehicles.
Okay?
Now, the interaction of the two, which you get by adding generics to an object-oriented language,
is pretty complicated.
Oh, and by the way, adding side effects as well.
Your question involving classes and superclasses and so forth is smack in that complicated world.