2 ms·
> As far as I can tell, D's templates and CTFE are as powerful as Zig's comptime You might very well be right. I didn't realize that D's templating was much di
by samatman 2y ago
> As far as I can tell, D's templates and CTFE are as powerful as Zig's comptime
You might very well be right. I didn't realize that D's templating was much different from C++, I don't know much about the language. I'll also note that the sibling comment you refer to wasn't posted when I wrote my reply.
I'll note that a careful reading of my post, and the one it was responding to, will reveal that I didn't claim that Zig was the only, or first, language to improve on C++ templates. Only that it's an example of a more modern language which has done so. But I'm also largely ignorant of the ins and outs of D.
It seems that you, however, are not. Here's an example of Zig building a data structure at comptime:
https://ziglang.org/documentation/0.12.0/std/#src/std/comptime_string_map.zig https://ziglang.org/documentation/0.12.0/std/#src/std/compti...
Is this an example of something D could do as well? If so, I'll be sure to give the language credit the next time the topic comes up.
- brabel 2y agoI admit I am no expert in D, I've written just a couple of hobby projects in it... but this sort of thing is actually much easier in D because it has associative arrays. About comptime, it seems to me it's trivial to do that in D. I started a little implementation of StringMap based losely on the Zig example, but I just did enough to show how D's templates and associative arrays could be used to implement that: https://godbolt.org/z/o7GoobbKb https://godbolt.org/z/o7GoobbKb You can see that the creation of the map does not generate any Assembly, so it's run at comptime: // comptime values! static immutable entries = ["foo": 1, "bar": 2, "zort": 3]; enum map = mapOf(entries); Notice that in D, "enum" can be used as a compile-time only variable, see https://dlang.org/spec/enum.html#manifest_constants https://dlang.org/spec/enum.html#manifest_constants The Assembly seems to be almost all related to the associative array implementation generated by the D compiler. EDIT : if you want to try to continue the impl, have a look at the D's template tutorial: https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md https://github.com/PhilippeSigaud/D-templates-tutorial/blob/... D templates can do pretty much anything.
- samatman 2y agoCool. > it has associative arrays Zig also has a variety of generic data structures in the standard library for this purpose, HashMap and ArrayHashMap and variants. The main limitation in the current implementation of comptime is that it can't handle heap allocations, so these can't be used at comptime yet. D has a garbage collector, which makes that sort of thing easier. It's also about fifteen years older, which also helps. That limitation won't be there forever, though, this is just the immaturity I referenced in my first post. But fair enough, you've convinced me: D is also a language which has improved on C++ templates using compile-time execution. Looks like C++ itself is another such language! The last time I used it was well before the later constexpr and immediate mode function features described here existed. https://en.wikipedia.org/wiki/Compile-time_function_execution https://en.wikipedia.org/wiki/Compile-time_function_executio...