Skip to content

Commit

Permalink
remove references to runtime generics
Browse files Browse the repository at this point in the history
  • Loading branch information
EinarBalan committed Nov 15, 2024
1 parent 7583f4d commit b66b937
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions lectures/13.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit b66b937

Please sign in to comment.