2 ms·
I 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 associativ
by brabel 2y ago
I 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...