This is an automated archive made by the Lemmit Bot.

The original was posted on /r/programminglanguages by /u/kizerkizer on 2024-10-16 02:30:24+00:00.


Though I’ve long known how to use generics/parameterized types, and been familiar with a set of examples which motivated their implementation (e.g., the ArrayList/container types in Java), I never really understood their necessity in a general sense, outside of that set of examples. I stumbled on this article reading up on the generics situation in C, but what stood out immediately to me was the following which elucidated for me the general motivation for generics (quoted from the article):

  • Subtype polymorphism allows code using an interface to be written in a generic style (by using a supertype rather than any of its subtypes); ad hoc and parametric polymorphism do not.
  • Parametric polymorphism allows code implementing an interface to be written in a generic style (by using parameterized types); ad hoc and subtype polymorphism instead require separate code for each type.

Wanted to share; maybe this will help someone else as well. Feel free to discuss (and perhaps educate me further).