|
|
Subscribe / Log in / New account

Comparison to Go?

Comparison to Go?

Posted Jan 5, 2025 18:26 UTC (Sun) by excors (subscriber, #95769)
In reply to: Comparison to Go? by tialaramex
Parent article: Preventing data races with Pony

> C++ programmers are used to templates, which are just text mangling again of course and so are duck typed, Rust won't let you do that.

This is mostly off-topic but: It's not just text mangling, once you get into metaprogramming. It's a powerful duck-typed language for writing programs that execute at compile-time, where the values in that language include ints, bools, and (crucially) C++ types. The use of C++ types as values is what makes it "meta" and distinguishes it from compile-time evaluation of regular C++ code, and also distinguishes it from the C preprocessor (which _is_ basically text mangling). The metaprogram can see all the types (including classes) in your non-meta code, and can output new types and functions for use by the non-meta code.

For example you can write something like `template<typename T> auto deref(T p) { if constexpr (std::is_pointer_v<T>) return deref(*p); else return p; }` which will convert e.g. `int***` to `int`. The metaprogram has input parameter `T` which is a C++ type; it can observe and manipulate that type in various ways (like with `is_pointer_v` to test if it's a pointer type), and in this case it outputs a C++ function that only includes the `*p` expression when it's legal to do so.

(Originally C++'s metaprogramming language was horrifically ugly, very hard to use, a pure functional language, and bore little resemblance to non-meta C++; it existed more by accident than by design. Nowadays with features like `if constexpr`, it's not always so bad, though it's often still quite bad.)

I think the closest equivalent functionality in Rust is procedural macros, but those are compile-time programs that operate over Rust tokens or ASTs, not over Rust types, so they're not really very similar. (I think no other language has anything very similar to C++ template metaprogramming, because you can get 90% of its functionality with very different designs that are 90% less insane.)


to post comments

Comparison to Go?

Posted Jan 5, 2025 18:41 UTC (Sun) by khim (subscriber, #9252) [Link]

> I think no other language has anything very similar to C++ template metaprogramming, because you can get 90% of its functionality with very different designs that are 90% less insane

That's funny because problem with C++ TMP is not the fact that it has too much functionality, but the fact that it has so little. There are nothing that's “very similar to C++ template metaprogramming” not because it's so powerful, but because it's so limited. C++ is slowly moving in the direction of Zig's comptime thus, is some sense, C++ is becoming like other languages (with metaprogramming in something that looks somewhat similar to the “regular”, “main” language), not the other way around.

Comparison to Go?

Posted Jan 9, 2025 18:31 UTC (Thu) by NYKevin (subscriber, #129325) [Link] (2 responses)

Hot take: Template metaprogramming is just Prolog with uglier syntax.

OK, OK, that's mostly wrong, but SFINAE is literally doing unification and backtracking, just like in Prolog.

Comparison to Go?

Posted Jan 9, 2025 18:41 UTC (Thu) by khim (subscriber, #9252) [Link] (1 responses)

Sure. But Rust's trait resolver is, essentially, the same thing just with a few more artificial limitations. But if you manage to confuse it enough… it works in the same way as C++ resolver.

Rust's type system and traits are to C++ type system and templates are more-or-less like TypeScript vs JavaScript: ultimately it's the exact same duck-typing, deep inside, only now with extra layer of typechecking on top.

The only substantial difference lies in the fact that with TypeScript you have an explicit “escape hatch”, but with Rust templates you need to fool the compiler to turn your generics into templates.

Comparison to Go?

Posted Jan 15, 2025 15:50 UTC (Wed) by taladar (subscriber, #68407) [Link]

The more substantial difference is that Rust doesn't routinely abuse the trait resolver in a way that produces error messages that could give small children nightmares while in C++ that is standard practice.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds