4 ms·
Can you eg. generate a struct with fields based on an existing struct or on a comptime array in D? Can you receive a structure literal of anonymous type and ite
by nikki93 6y ago
Can you eg. generate a struct with fields based on an existing struct or on a comptime array in D? Can you receive a structure literal of anonymous type and iterate over it at compile time? (curious bc. these were a few things I found straightforward in Zig, and in C++ the main proposals involve tons of new metaclasses and reflection features for)
- mhh__ 6y agoYes to all points. D doesn't go the C++ route, the way code like this works is via generating new code (in text) at compile time which the compiler then compiles (duh) and replaces the mixin statement within. Here's an annotated example of these features in use https://run.dlang.io/is/2b5zgC https://run.dlang.io/is/2b5zgC
- fithisux 6y agoBrilliant.
- Matheus28 6y agoThat is beautiful. I wish C++ had something like that... Perhaps in addition to its current template system. Having a separate build pass to generate files is annoying.
- pjmlp 6y agoIt does, via constexpr, which just got more expressive in C++20, and is the basis for the ongoing compile time reflection work on C++23. Code injection might eventually come later via Microsoft's ongoing research in C++ metaclasses. "Dynamic Polymorphism with Metaclasses and Code Injection" https://www.youtube.com/watch?v=8c6BAQcYF_E https://www.youtube.com/watch?v=8c6BAQcYF_E
- Matheus28 6y agoThat looks a lot more complicated than D's simple "output text that gets fed back into the compiler". But admittedly I only quickly jumped around the video. Hopefully it works well, though.
- pjmlp 6y agoTrue, but a programming language is more than just syntax and for better or worse, the ecosystem is what matters.
- mhh__ 6y agoThat's what I meant by doesn't go the C++ way. The standards committee over complicate everything (concepts still haven't landed yet for example, D has D a better more flexible system 15 years ago I think)
- pjmlp 6y agoAnd no ecosystem to speak off, while that standards committee is making sure that GPGPU programming is the next killer application for C++.
- mhh__ 6y agoHow are modules coming along then? C++ has succeeded despite the committee not because of it.
- pjmlp 6y agoAlready available as experimental on VC++, and approved as part of C++20. As proven by every failed attempt to replace languages with industry standards, plenty of companies do care about ISO and ECMA language standards. D is a nice language, but spends too much time focusing on the language and its cool metaprograming capabilities, without focus on the ecosystem. Slowly Java, C#, C++ will have the features that made D unique, with the plus side of 20 - 40 years of libraries, IDE support and industry certifications.
- nikki93 6y agoCool! Nim is related here, where instead of text manipulation you're doing AST manipulation with quasi-quoting (like in many lisps), eg. https://nim-lang.org/docs/tut3.html#introduction-building-your-first-macro https://nim-lang.org/docs/tut3.html#introduction-building-yo....