5 ms·
What are some examples of other language features that comptime makes redundant?
by forks 2y ago
What are some examples of other language features that comptime makes redundant?
- dhruvrajvanshi 2y agoIt's generic system for example, is built on top of comptime. A generic struct is just a function that takes a type as an argument and returns a struct. ``` fn Vec(comptime T: anytype) { return struct { // ... } } ``` IMO having a first class generic type parameter syntax is better but this demonstrates OP's point.
- Wumpnot 2y agoIt just looks like C++ templates with a slightly different syntax ..
- azakai 2y agoExactly, the point is that C++ added templates as a huge new language feature, while in Zig it is just one of the things that is immediately possible thanks to comptime.
- throwawaymaths 2y agowell, not quite since you can pass non-type things of generally any level of data type complexity (as long as it's comptime-valid, which only excludes certain types of mutation), and do stuff with them that you couldnt in c++. fn MyType(T: type, comptime tag: [] u8, comptime count: usize) type { const capitalized = some_module.capitalize(tag); return struct{ fn name() []const u8 { return capitalized; } array: [count]T, }; } for example
- Wumpnot 2y agoThat example looks easy enough to replicate in C++ with consteval + template, basically the same except a few minor syntax changes.
- Maxatar 2y agoYou absolutely can't do that in C++ with consteval + template. C++ would need support for reflection to do that, and maybe it will get it in 10 years, maybe not, but as of today this would not be possible. Furthermore, the original argument wasn't about whether something can or can't be done in C++, it was that this one feature in Zig subsumes what would require a multitude of features from C++, such as consteval, templates, SFINAE, type traits, so on so forth... Instead of having all these disparate features all of which work in subtly different ways, you have one single feature that unifies all of this functionality together.
- steveklabnik 2y agoYou're not wrong in general here, but C++ is going to get the core of reflection in C++26. I'm not sure enough of the details to know if it supports doing this, however. Rust on the other hand... that might be ten years.
- pcwalton 2y agoThere are various reflection crates available on crates.io, as you know.
- steveklabnik 2y agoYeah, I always wished that the reflect crate got further along than it has. I still think that language support us important, but unfortunately due to what happened, I suspect that will take a long time. And that’s disappointing.
- pcwalton 2y agoWe Bevy users use Rust reflection on a daily basis, and are very happy with it :) I agree it'd be nice if it weren't confined to our community, though.
- 2y ago
- SkiFire13 2y agoNit: comptime does not replace a proper generic system (i.e. a polymorphic type system), but acts more like a templating system (like the one in C++).
- pron 2y agoGenerics, interfaces/traits/concepts, macros, conditional compilation, const functions/constexpr. These are four or five different features in C++ or Rust, some of which are quite complex, all expressible as one simple construct: comptime.
- kobebrookskC3 2y agohow does zig express a trait like Send https://doc.rust-lang.org/std/marker/trait.Send.html https://doc.rust-lang.org/std/marker/trait.Send.html which ensures that values can safely be moved to another thread, for example when spawning a thread? https://doc.rust-lang.org/std/thread/fn.spawn.html https://doc.rust-lang.org/std/thread/fn.spawn.html
- throwawaymaths 2y agoif you want that you will need a proof checker or something.
- pcwalton 2y agoSuch as the one Rust provides, and that Zig cannot, due to not being memory safe.
- rc00 2y agoIt sounds like you haven't seen projects like this yet: https://github.com/ityonemo/clr https://github.com/ityonemo/clr They're popping up all over. For some reason, Zig folk want Rust things and Rust folk want Zig things.
- pcwalton 2y agoI have seen projects like that, and they're a prime example of what I mean when I want to see arguments in favor of Zig that rely on what Zig can do now, and not what it could potentially do in the future. Ten years ago, C++ folks were also promising memory safety with the ISO C++ Core Guidelines; you don't hear much about that anymore, because it turns out it can't be done while keeping the resulting language C++ (for example, seanbax's work for C++, which is far more advanced than this project, is really awesome, but is essentially a different language).