diff --git a/lectures/13.md b/lectures/13.md index f988ded..706c7ea 100644 --- a/lectures/13.md +++ b/lectures/13.md @@ -59,10 +59,6 @@ Ad-hoc polymorphism isn't possible in dynamically typed languages. This is becau With parametric polymorphism, we define a single, parameterized version of a class or function that can operate on many, potentially unrelated types. Parameteric polymorphism is implemented in two major ways, templates or generics. The syntax for the two may look similar, but they're implemented in entirely different ways - with big implications! -{: .note } -People often colloquially use "generics" to cover the entire field of parametric polymorphism, and call templates "compile-time generics" and generics "run-time generics". For this class, we'll use the templates vs generics naming convention. - - #### Templates Templates do almost all of the work at compile-time. In languages that use templates (like C++ or Rust), the compiler will see all the types that are used in each template. For each invocation, the compiler will do some type-checking. Then, it will generate a concrete version of the function/class by substituting in the type parameter. Finally, it'll continue compiling the code "as normal". After template substitution, the code is "normal" C++ - there's nothing special about it! @@ -131,7 +127,7 @@ In Eggert's 131, we would have learned quite a bit more about macros (especially #### Generics -Generics (or "runtime generics") take a different approach. Instead of generating a new function for each parametrized type, we compile just one "version" of the generic function or class - independent of the types that actually use our generics. +Generics take a different approach. Instead of generating a new function for each parametrized type, we compile just one "version" of the generic function or class - independent of the types that actually use our generics. Because of this, the generic can't make assumptions about what types might be using it! So, you can only do "generic operations" - hence the name.