Also creating nested libs as separate compilation units is so difficult with cmake etc I rarely seen a large C++ project do this... it is much easier with Cargo.
I'm sorry I was a bit snarky. But the thing is that the Rust evangelists don't understand the domains and types of large scale projects that C++ is used in and how those systems are built. Yet the they come along and keep pushing for Rust without understanding why C++ developers aren't switching and wave their hands at the issues we have with Rust.
Show me a successful multi million line Rust project. Maybe throw in a complex UI. Maybe some GPU computation.
FWIW Rust is great for high performance web services, not so much UI stuff. C++ would be better for that.
If you do the same in rust, you need access to the module's implementation for monomorphization. You might skip the type checking of a function of the implementation or even cache the generated code for a monomorphization as optimizations, but generally you have to treat every function as often as it is used. So the best rust could do is akin to C++ precompiled headers whereas a language like OCaml can simply compile every module exactly once.
Note: OCaml pays for this elegance. Its uniform object representation is less efficient than what rust does. It's a trade-off that the rust designers consciously made, I think.
I think using features of the language is fair game
btw, don't C++ templates also use monomorphization? I agree C++'s implementation is "lower level" (from a compiler perspective) than Rust generics, but that's one of Rust's selling features
Of course one can redefine the meaning of "polymorphism" and "monomorphization" to fit C++, but then these words lose nearly all meaning.
In my plebeian brain I just thought “monomorphization = static dispatch” / 0-cost abstraction (vs. just writing multiple versions of a function), which both Rust generics and C++ templates fall into.
Now I see that “monomorphization” has a specific definition in FPL
https://en.m.wikipedia.org/wiki/Parametric_polymorphism
Note that it's called "parametric" because OO folks stole the word polymorphism for a completely different thing.
The idea of a polymorphic function is that it acts the same for all possible input values (for at least one parameter). This is captured by its type. If you look at a polymorphic function (say "f(x) = x" with the type "for all 'a. 'a -> 'a ") you can check that type once and you know that monomorphization works for any possible input.
Monomorphization is then the process to replace 'a with a concrete type to facilitate code generation. What it actually does, depends on your language of course. It might for instance decide how to pass arguments or compute offsets from record labels. Crucially, monomorphization in this sense never fails, at least not because you input the wrong type. The user doesn't need to know that monomorphization happens at all. It's an internal design choice of the compiler.
If you look at C++, template specialization is very visible. The template that represents the identity function doesn't have a type. Only after specialization, the user learns whether it was successful or not (e.g., because copying the type is not possible).
I never worked with concepts myself, but to my understanding, they are an attempt to generate more useful error messages. As far as I know, the concept declaration of a template might simply be wrong, there's no automatic check that guarantees that a template can be instantiated if I adhere to the contract, right? So the type check still has to happen after specialization and the user has to consider specialization in their mental model of the code.
- Templates (compile time), which are generic bits of code that are monomorphized over every combination of template parameters that they're used with.
- Virtualization (runtime), classic OO style polymorphism with a VTable (I think this similar to Rust's dyn trait?).
I still think that C++ templates are different than rust macros as they are not a separate pass from type checking.
Either that, or switch to Circle I guess.
Now can we automate writing archetypes from concepts (and viceversa)? Maybe in C++64 when we finally get static reflection.
I really hate how C++/WinRT used the "C++ reflection is around the corner" excuse to kill C++/CX and downgrade the COM development experience back to pre-.NET days.
Apparently it isn't around the corner.
However with type traits and a bit of constexpr if (requires {....}) it is possible to have a kind of poor man's compile time reflection.
The only thing missing was effort into the idea to make it the default and able to be supported long term. You need a stable WASM ABI that won’t change across rustc versions and for someone to add a runtime to rustc.
Dtolnay (the author of the Watt concept) recently put out a message looking for help putting together an RFC. https://twitter.com/davidtolnay/status/1574913813400330241
Simply put: Rust doesn't offer an abstraction past type checking. Rust could potentially introduce its equivalent of PCH, or maybe it already does so in some internal cache, but as long as it uses monomorphization and no PhD student comes up with a clever trick to combine that with separate compilation it will scale worse.
That being said, maybe a more fair comparison wouldn't involve polymorphism in the rust code? C++ doesn't really have polymorphism, it uses templates for a similar feature. So what happens if someone replaces rusts polymorphic functions with macros?