4 ms·
I'm just happy that D was mentioned. Yay, D! Also, D's metaprogramming is an utter joy, almost lisp-like, without the craziness of C++ templates. Give it a try!
by jordigh 8y ago
I'm just happy that D was mentioned. Yay, D! Also, D's metaprogramming is an utter joy, almost lisp-like, without the craziness of C++ templates. Give it a try!
An intro to one ingredient of the magic of D metaprogramming, compile-time function evaluation:
https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe https://tour.dlang.org/tour/en/gems/compile-time-function-ev...
An example of the kind of introspection available in the D stdlib:
https://wiki.dlang.org/Finding_all_Functions_in_a_Module https://wiki.dlang.org/Finding_all_Functions_in_a_Module
This one's really cool, a library that lets you build an entire grammar of your choice and evaluate it at compile time:
https://github.com/PhilippeSigaud/Pegged https://github.com/PhilippeSigaud/Pegged
Nim's metaprogramming seems quite similar, and I sometimes wonder if perhaps it took some inspiration from D.
- nerdponx 8y agoNim appears to have taken a lot of inspiration from D in general, eg UFCS.
- VHRanger 8y agoI wish more languages had UFCS. It makes for really readable code.
- rattray 8y agoFor others unfamiliar with the acronym, it looks like it's Universal Function Call Syntax - en.m.wikipedia.org/wiki/Uniform_Function_Call_Syntax Lets you call top-level funtions as if they were metnks, with the receiver as the first argument. For example, these two are equivalent: add(x, y) x.add(y) As an aside, I added a similar feature to my compile-to-js lang (http://lightscript.org http://lightscript.org) and have found it to be a delightful feature. I hope an equivalent passes TC39.
- 542458 8y ago> compile-time function evaluation Sorry, potentially stupid question incoming: I thought this was pretty standard for c++ optimizing compilers. What makes D's version of this special?
- VHRanger 8y agoIt means you can run compile time programs without resorting to crazy template metaprogramming
- zamalek 8y agoC++ now has constexpr, so that pro is now shared by both C++ and D, somewhat (ignoring templates, D is still more versatile). That being said, C++ template metaprogramming can be pretty elegant[1] (there was a video where she describes the template functional style, but it seems to have been deleted). [1]: https://github.com/hanickadot/compile-time-regular-expressions https://github.com/hanickadot/compile-time-regular-expressio...
- WalterBright 8y agoconstexpr accretes more power with every iteration, but it's still got a ways to go: https://en.cppreference.com/w/cpp/language/constexpr https://en.cppreference.com/w/cpp/language/constexpr https://dlang.org/spec/function.html#interpretation https://dlang.org/spec/function.html#interpretation
- int_19h 8y agoIt's not an optimization in this case - it's a mechanism that produces what is semantically a compile-time constant, and can therefore be used in any context in which one is required by the language. C++ has this with constexpr, but it's not as rich in terms of things that it can do. In D, IIRC, pretty much anything goes.
- yvdriess 8y agoIt is ad-hoc in C++, templates weren't designed for general compile time meta-programming. It took several recent changes to the standard to make it a little more palatable with static expressions and general template improvements.