Re:C++ Pattern Matching Proposal [pdf]
by tick_tock_tick on Friday January 03, 2020 @04:16PM
(#21950476)
You can always role dynamics dispatch yourself
[ Parent
| on HN ]
Dynamic dispatch is in the language too.
use core::fmt::Display;
fn show_monomorphic<T: Display>(first: T, second: T) {
println!("{} then {}", first, second);
}
fn show_polymorphic(first: &dyn Display, second: &dyn Display) {
println!("{} then {}", first, second);
}
pub fn main() {
show_monomorphic(17, 23);
show_monomorphic("fnord", "slack");
show_polymorphic(&42, &"quirkafleeg"); // mixed types!
}
There are things you can do with each that you can't do with the other, but they are often both viable choices.If you're doing dynamic dispatch then there is still code that's living for each specialization. Sort of solves linking because symbols don't have to be generated and compile times because you hand write the code.
To be fair, I'm only considering basic data structures and algorithms where you can get away with something like foo(void *ptr, size_t size). Maybe generics is too broad a term for that.