Polymorphism with Unions
summary
Argues that tagged unions replace brittle class hierarchies by collapsing finals into a value type with a vtable-like tag, shows a C++14 implementation on GitHub that uses overloaded lambdas to implement multi-method dispatch and a configurable nullable variant state. Demonstrations include a compiler-style example and a small interpreter, the implementation actually runs, and measured dispatch costs are nontrivial (roughly up to half as fast or somewhat worse than virtual calls) but acceptable outside hot loops.
AI-written summary · about
chapters (12)
- 0:00Start
- 0:39Class Hierarchy
- 2:33Visitor Pattern
- 14:00Overloaded Lambda
- 16:51Tagged Union
- 25:45Group Virtual Behaviors out of Animals
- 40:36Multi Dispatch
- 43:12Difference between a Vector of Unions or a Tuple
- 48:31Options for Improving the Performance
- 50:27And We Can Have a Forward Declaration of Expression at that Point That We Finalize Eventually with the Union So Exactly the Same Thing You Would Do if It Weren't Union Yeah We Do in Fact so My My Backups Live if I Had Slides Here's an Entire Language Interpreter Written with Yeah Simple Calculator Written with a Union for Values and Union for Expressions and It Actually Works So and It's Actually Pretty Short Considering Yeah Well I Didn't Have As Much Time before this Conference as I Thought I Was Going To Huh I Try To Remember To Go In and Comment Things and So I Leave Nag Messages for Myself
- 53:03It Would Be Possible To Do that the Problem Is I Need some State To Copy or Steal To Initialize the Variant So if You Create a Dog as an R-Value Reference I Can Move It and Then It's My Variant Dog if You Just Say I Want a Dog by Name Then I Need some Kind of a Factory so We Could Build a Factory Pattern around this As Well It Would Just Be Something That Sits on Top It Wouldn't Really Change this At All I Can Talk to You about It Afterward if You like Anybody Else I Think Okay the Question Is whether It Works like Pattern Matching and Is Therefore Not Order Dependent It Is Not Order Dependent It Works like Pattern
- 55:20It's It's Doable the Point Is Sometimes You'Re Going To Do Inheritance for Classification for Behavior and Sometimes You'Re Going To Do It for Construction of the Type Basically It's a Form of Aggregation so You'Re Going To Stick Things in but Not Be Them for the Purpose of an Algorithm That's Fine You Can You Can Certainly Do that and Then those Bases Later You Could Match on if You Wanted To with the with an Overloaded Lambda but You Could Overlook You Could Do the Overloaded Lambda on Anything That Would Be Statically Discernibly Different about the Type Absolutely So for Example though the Right like I Want To Stream this Variant out the Simplest Case Right You Just Have a Single Auto That Whatever It Is I Shove It onto an Out Stream and It's Fine Right because They all They all Provide Their Own Overloads for Doing that
related talks
01h 18mMeta Polymorphism
49mA Journey Into Non-Virtual Polymorphism in C++
01h 01mUsing Modern C++ to Eliminate Virtual Functions
36mThe language that isn't
28mRFC: In Order to Form a More Perfect `union`
01h 05mExpressing Implementation Sameness and Similarity - Polymorphism in Modern C++
23mFunctional Programming in C++ (Run time vs Compile time types)
01h 09mFunctional Programming in C++
