4 ms·
Compile-Time Sort in D
- wbradley 5y agoThe idea of static meaning “fully evaluate this at compile time” in D is a good one.
- MauranKilom 5y agoObligatory C++ comparison: C++ has constexpr since C++11 and constinit since C++20. They mean more or less the same when applied to variables ("must be evaluated at compile-time"), but constinit does not imply constness.
- mhh__ 5y agoD evaluates until it hits something it can't manipulate at compile time whereas constexpr requires things to be annotated as such, so the difference is slightly more subtle.
- pjmlp 5y agoWhile true, Circle C++ compiler works as D does, although that is not standard, thus D does win in expressiveness.
- dom96 5y agoSame code in Nim: import algorithm const a = [ 3, 1, 2, 4, 0 ] static: echo(sorted(a))
- melissalobos 5y agoNot sure why this was downvoted, it was a valid comparison from someone who is highly involved in Nim development. Nim compiles to C(by default, other backends available) and the static means it is done at compile time. The article itself is a D implementation of something done in C++ to show how D does it, and how much simpler it is. The Nim example is right in line with this.
- cyber_kinetist 5y agoNim can run almost any code (that doesn’t interface with C FFI) at compile time, since in addition to a compiler they have a bytecode VM that executes a large subset of the language. (https://nim-lang.org/docs/nims.html https://nim-lang.org/docs/nims.html) Really cool stuff, the only other language that I’ve seen doing this approach is Jai, which is sadly still in closed development. (Jai’s CTE is much grander in scope, in that it would be able to do anything normal code can do.) C++ (gcc and clang) and D’s compile-time evaluation is a bit more limited in scope, and still uses a tree-walk interpreter, although D might also migrate to a bytecode VM someday (https://dlang.org/blog/2017/04/10/the-new-ctfe-engine/ https://dlang.org/blog/2017/04/10/the-new-ctfe-engine/)
- alecthomas 5y agoZig does this too I believe: https://kristoff.it/blog/what-is-zig-comptime/ https://kristoff.it/blog/what-is-zig-comptime/
- cyber_kinetist 5y agoGood catch, forgot about Zig. Although it doesn’t have a fast bytecode VM dedicated for running comptime, Andrew has stated that it will achieve CPython-level performance once the self-hosted compiler is finished, so things might become better. https://github.com/ziglang/zig/issues/4055 https://github.com/ziglang/zig/issues/4055
- WalterBright 5y agoD's doesn't allow global variables or pointer arithmetic or monkey business like converting integers to pointers, the rest is supported.
- d110af5ccf 5y ago> the only other language that I’ve seen doing this approach is Jai Many (most?) Lisps have had full arbitrary CTE since ... I'm not sure, probably before C existed?
- 5y ago
- CyberShadow 5y ago(2017) More impressive: manipulating strings at compile-time. Even more impressive: using string manipulation at compile-time to build valid D code, then use string mixins to inject it into the current program, passing it on to the compiler to be compiled as part of the program being built. This is done by std.regex to compile regular expressions to native code during compilation. Even further impressive: using a string import to read a DSL from disk, transpiling it into D code, and passing it on to the compiler. This is used by Vibe.d to compile HTML templates to native code. Granted, all this is fairly old news, and a few other languages have since caught up.
- dmead 5y agopretty much lisp macros with the constraints of a compile language. haskell templates are similar.
- Kranar 5y agoMost people don't compare D to Lisp or other languages with excellent metaprogramming. D is almost exclusively compared to C++ and C++ went through a painful metaprogramming phase of its own in the 2000s. While C++ is not by any means a great meta-language, it's improved considerably since that time. D still has some interesting metaprogramming features, but on the whole I don't think it's enough to justify the rest of the baggage that comes with the language. Buggy implementations, releases that constantly break valid source code and make it a pain to work with dependencies along with a poor/unscalable garbage collector. The ability to write a string literal that contains D code, perform raw string manipulations on it and then compile the result certainly sounds neat on paper and if you're working on a solo project it can be a fun exercise to blog about, but beyond the novelty you'd hardly find a mature or reliable codebase written by a team of professionals using hacks like these.
- mhh__ 5y agoNo one does manipulation of D code as strings, and no one is really telling you to IMO. I do however work on a large-ish codebase with many professionals developers which makes extensive use of metaprogramming. We also don't have particularly big issues with moving from version to version so how old is your hearsay? C++ doesn't even have proper modules yet, it's still not as good as D in more regards than just metaprogramming.
- davydog187 5y agoAfter spending time with Elixir which has hygienic macros and the whole language available to you at compile-time, this is no longer interesting to the old C++ dev in me
- toomanydoubts 5y agoYou'll have lots of fun in Haskell should you decide to learn it.
- MauranKilom 5y agoYou can get virtually the same effect with marking functions `constexpr` in C++. Yes, you need to do it explicitly, and yes, you can't call functions that aren't marked `constexpr` inside, but it's more or less the same - just opt-in. In fact, since C++20, you don't need any template metaprogramming for compile-time sorting - std::sort is constexpr! [0] Demo: https://godbolt.org/z/6M1Wqc31W https://godbolt.org/z/6M1Wqc31W [0]: https://en.cppreference.com/w/cpp/algorithm/sort https://en.cppreference.com/w/cpp/algorithm/sort
- cyber_kinetist 5y agoI’m worried about the compile-time performance though. C++ compilers currently use direct tree-walking on the AST to interpret code, which is going to be horrendously slow once you go past a certain number of elements.
- csb6 5y agoI’m not sure what GCC and MSVC are doing, but Clang [1] at least is in the process of developing a bytecode interpreter for running constexpr code more efficiently. It seems like it hasn’t been actively worked on in quite some time, unfortunately, based on the commit logs. [1] https://clang.llvm.org/docs/ConstantInterpreter.html https://clang.llvm.org/docs/ConstantInterpreter.html
- WalterBright 5y agoI like to use compile time code to generate fixed tables, like these: https://github.com/dlang/dmd/blob/master/src/dmd/backend/oper.d#L595 https://github.com/dlang/dmd/blob/master/src/dmd/backend/ope... Notice how a static immutable array is created by a lambda at compile time. The lambda never appears in the executable. Before D, I'd use a separate C executable to generate the tables, which were then #include'd. The lambdas can also include sanity checks: https://github.com/dlang/dmd/blob/master/src/dmd/backend/oper.d#L538 https://github.com/dlang/dmd/blob/master/src/dmd/backend/ope...
- deleted 5y ago[deleted]
- bjoli 5y agoAnother example of compile time evaluation: one of my favourite things about the guile repl is the ,opt[imize] command at the repl. You run it on a piece of code and it shows you the result of macro expansion, inlining, DCE, some other things, and most notably partial evaluation. One example of how I use it can be found here: https://git.sr.ht/~bjoli/goof-loop/#speed https://git.sr.ht/~bjoli/goof-loop/#speed in the first example where I contrast ,expand (macro expansion) and ,opt (the source->source optimizer.
- faisal_ksa 5y agoD has many cool features. I always wonder why it is not (more?) popular?