Skip to content

First pass at polymorphism improvements - #3264

Draft
randomPoison wants to merge 2 commits into
google:mainfrom
randomPoison:legare/polymorphism-rework
Draft

First pass at polymorphism improvements#3264
randomPoison wants to merge 2 commits into
google:mainfrom
randomPoison:legare/polymorphism-rework

Conversation

@randomPoison

Copy link
Copy Markdown
Contributor

After teaching the Idiomatic course a couple of time, I have some ideas for how to iterate on and improve the polymorphism section. The main things I want to address are:

  • Differentiate between "static" polymorphism (generics) and "dynamic" polymorphism (enums and dyn), since those serve different purposes: Generics are primarily a tool for code reuse and building abstractions over types, enums and dyn provide tools for dynamically handling different types at runtime in a uniform way.
  • Emphasize enums more heavily. The current slides don't really mention enums, which I think is a big oversight since enums are our primary tool for doing dynamic polymorphism.
  • Compare enums and dyn to help students reason about which one is the best solution for any given problem.
  • Shift material out from the OOP section into more dedicated sections on generics, enums, and dyn. I think we can then flesh out the OOP section further by having it talk about how inheritance is used to solve certain problems and then talk about how we solve those same problems in Rust.

The things that this draft PR does currently are:

  • Break things up into sections on generics, enums, and dyn.
  • Various non-OOP-specific slides moved into the new sections.
  • Add new intro slides contextualizing polymorphism and how it comes up in a static language like Rust, and introducing our 3 flavors of polymorphism.
  • Add slides on enums and how they're used for polymorphism.
  • Add slide directly comparing enums and dyn.

I intend to teach from these slides in my next couple of classes and iterate on them more based on how that goes, but let me know if this direction makes sense or if you have ideas for further improvements or additions~! 🦀

- Break things up into sections on generics, enums, and dyn.
- Various non-OOP-specific slides moved into the new sections.
- Add new intro slides contextualizing polymorphism and how it comes up in a static language like Rust, and introducing our 3 flavors of polymorphism.
- Add slide directly comparing enums and dyn.
- Add slides on enums and how they're used for polymorphism.
@fw-immunant
fw-immunant self-requested a review August 26, 2026 16:05

@fw-immunant fw-immunant left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Various comments inline.


fn main() {
print_value(123);
// print_value("hello"); // 🛠️❌ Mismatched types!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The emoji here are kind of distracting IMO. Relatedly, I think we should standardize how we denote that lines in examples would cause errors if uncommented, because we do so in quite a few places. Only one of them (src/lifetimes/returning_borrows.md) uses this emoji style. Other places say "// ERROR: <explanation>", "// Error, <explanation>", or don't say that the line will generate an error explicitly at all (e.g. "// takes_u32(y);"). Within the idiomatic course there are a few variants on the emoji combination, but most places use "❌🔨" rather than "🛠️❌". Personally, I think a simple red X is enough to suggest "error"--the hammer or hammer+wrench seem to suggest something being fixed as well, which is the opposite of what these lines do.

Comment on lines +15 to +16
But Rust's type system is extremely static, meaning that by default a function's
arguments are limited to exactly the type declared in the function signature:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"extremely" static isn't really a thing--whether a language does typechecking before translating/running code is a binary distinction.

Suggested change
But Rust's type system is extremely static, meaning that by default a function's
arguments are limited to exactly the type declared in the function signature:
But Rust's static type system requires us to specify upfront the types of our functions' arguments:

We could then say "We can make this function more flexible, like the Python version, by using generics."

Comment on lines +31 to +34
- If you are coming from a dynamic language like Python, the concept of
polymorphism may be new to you because in dynamic languages everything is
inherently polymorphic. But Rust is a very statically-typed language, meaning
the compiler heavily restricts what types can be used where.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- If you are coming from a dynamic language like Python, the concept of
polymorphism may be new to you because in dynamic languages everything is
inherently polymorphic. But Rust is a very statically-typed language, meaning
the compiler heavily restricts what types can be used where.
- If you are coming from a dynamically-typed language like Python, the concept
of polymorphism may be new to you because in these languages types are just
one part of the contract between functions and their calling context, which
is left to negotiation by programmers rather than enforced by the language.
But in Rust, a statically-typed language, the compiler must always be able to
determine precise types for all variables, either through inference (within a
function) or from explicit annotations.

@@ -0,0 +1,31 @@
# Kinds of Polymorphism

In Rust we have 3 different mechanism for doing polymorphism:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In Rust we have 3 different mechanism for doing polymorphism:
In Rust we have 3 different mechanisms for writing polymorphic code:

Comment on lines -29 to -34
- Dynamic Dispatch is a tool in Object Oriented Programming that is often used
in places where one needs to care more about the behavior of a type than what
the type is.

In OOP languages, dynamic dispatch is often an _implicit_ process and not
something you can opt out of.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for dropping this prose? In my mind this explanation is useful we assume if our audience is coming from a particularly OO-heavy background, e.g. C++ or Java, where subclassing is pervasive. But on the other hand, that difference is also covered in Fundamentals when we talk about dynamic dispatch and memory layouts of trait objects.

Comment on lines +1 to +4
# Re-exposing Traits

Sometimes we use an enum to abstract over multiple types that implement the same
trait, and want the enum to also re-expose the trait's interface.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the perspective of "idiomatic Rust", maybe we should mention crates used to automate this pattern, e.g. enum_dispatch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants