5 ms·
Rust's generics don't have overhead and the error messages are at least as sane as Java's. I think the problem is with how C++ implemented its templates.
by weberc2 8y ago
Rust's generics don't have overhead and the error messages are at least as sane as Java's. I think the problem is with how C++ implemented its templates.
- burntsushi 8y agoC++'s templates are more expressive than Rust's generics. Rust is adding stuff to try to catch up (specialization, const generics). You might see the extra power as an anti-feature, and that's fine, but it's undeniably useful to lots of folks and is undeniably a trade off.
- masklinn 8y agoBut doesn't the fact that Rust is managing to add these features mean the lack thereof is not a limitation of classical parametric polymorphism? Not that they're necessarily trivial to integrate, especially when the people working on it would rather try for the stars of genericity (e.g. dependent types versus const generics).
- burntsushi 8y agoI would be surprised if Rust's type system winds up supporting everything C++ templates do. But maybe I'll be wrong, or the only things not covered will be niche and easily worked around. I don't know.
- saghm 8y agoI feel like part of the reason that I don't run into issues with Rust having less expressive generics than C++ is that Rust has a ton of other useful meta-programming features; a lot of the crazy things I've seen solved by C++ templates can be solved in Rust via things like proc macros.
- htfy96 8y agoJust wondering if it's possible to implement something like expression template used in Eigen3 in Rust. C++'s template can be used in more than generics.
- weberc2 8y agoSure, but those are different tradeoffs than those listed above. And some of the warts with C++ templates might not be inherent to supporting the features that C++ templates offer over Rust generics--for example, what features are enabled by error messages that point into the expansion of a template rather than a type error at the template itself?
- burntsushi 8y agoThey aren't different. The last bullet point in munificient's comment is a point about expressiveness. I honestly don't know what point you're trying to make here. This really shouldn't be controversial.
- weberc2 8y agoFair point, I missed the last bullet. To me, munificent's comment reads like a false dichotomy--you either have the Java's problems or C++'s problems. I was pointing out that there are other systems that largely have neither--namely that you can have template-based generics that perform better than Java and are friendlier than C++. He may well not have meant it that way, but that's the context for understanding my posts.
- stcredzero 8y agoC++'s templates are more expressive than Rust's generics 80/20 rule applies here. 80% of the boilerplate can be eliminated with 20% of the expressiveness.
- burntsushi 8y agoYes. I agree. Some might even argue that this applies to Go itself: its builtin generics get you a lot without bringing in the full baggage of user-defined generics. There's really nothing in my comment that suggests I think otherwise, so I'm a bit confused by your comment.
- stcredzero 8y agoI'm expanding on your comment about trade-offs. I tend to like nuanced views of cost-benefit trade-offs in language design. It is a complicated subject, after all.
- vvanders 8y agoYeah, but there are some limitations with Rust's generics around numeric types. You can't declare generics with arrays of a fixed size(which is really common in performance sensitive code) so there are trade-offs. I understand that it's being worked on. I won't disagree that C++ templates are sharp, pointy tools but there are some things that they enable which you can't get in other languages.
- weberc2 8y agoYeah, my point was not "Rust generics are strictly better than C++ templates in all cases", but rather that the GP made it sound like you have to choose between C++ templates and type erasure and Rust proves that's a false dichotomy. Sorry if I was confusing.
- dleslie 8y agoI think it's how C++ is compiled. Until recently, there was no way to forward declare templates and that required all sorts of hoop jumping or concessions to overcome. Similarly, the existence of header files at all is probably an anti-feature.
- orthoxerox 8y ago> and the error messages are at least as sane as Java's. Last time I made a mistake stringing Tokio promises together, the error messages were straight from the C++ hell.