Lurker News

Comments (191) 32 threads

Show all comments

Showing thread 26 of 32

crazypython#
DLang can sort an array at compile-time using a normal function: (CTFE)

  void main()
  {
      import std.algorithm, std.conv, std.stdio;

      "Starting program".writeln;

      // Sort a constant declaration at Compile-Time
      enum a = [ 3, 1, 2, 4, 0 ];
      static immutable b = sort(a);

      // Print the result _during_ compilation
      pragma(msg, text("Finished compilation: ", b));
  }
It can also reflect at compile time on user-defined annotations, which can store arbitrary metadata on any symbol, to generate arbitrary code at compile time. They serve a similar role to procedural macros from Rust, but they're easier to use than the earlier. Thanks to mixin templates, you can inject arbitrary symbols, making D's UDAs just as flexible as Python's decorators.