News for hackers. Stuff that matters.
Re:Idioms for the D Programming Language by jcelerier on Thursday February 11, 2021 @05:39PM (#26108372)
> One thing C++ will never have is D's `static if`, which allows injecting declarations into scope

don't be so sure

https://github.com/lock3/meta/wiki/Metaprogramming-Introduct...

a lot of it is already implemented: https://cppx.godbolt.org/z/4arx6T

Re:Idioms for the D Programming Language by acehreli on Thursday February 11, 2021 @07:50PM (#26109586)
Thanks for letting me know. This seems to be far from being adopted into C++, if at all, right? Is the C++ community behind this effort?
Re:Idioms for the D Programming Language by jcelerier on Friday February 12, 2021 @02:28AM (#26111422)
It has been proposed by Herb Sutter a few years ago and has been worked on since by Andrew Dutton, so yes the c++ community is definitely behind it.

See e.g. https://youtu.be/8c6BAQcYF_E

Re:Idioms for the D Programming Language by mhh__ on Thursday February 11, 2021 @05:46PM (#26108465)
Seems horrifically complicated?
Re:Idioms for the D Programming Language by jcelerier on Friday February 12, 2021 @05:53AM (#26112315)
it allows to generate code at basically every scope and to create your own "base language elements" like "struct", "class". AFAIK D does not allow for this, does it ?
Re:Idioms for the D Programming Language by acehreli on Friday February 12, 2021 @11:26AM (#26115341)
D's templates has always been very strong and useful compared to C++'s.

The following example is similar to C macros because it generates code as string and mixes it in as code but you can combine templates as well. This example generates a struct definition.

(Edit: formatting)

  import std; // Entire package for brevity
  
  auto memberDef(string type, string name) {
    return format!"  %s %s;"(type, name);
  }
  
  auto memberDefs(string[] members...) {
    return (members
            .chunks(2)
            .map!(pair => memberDef(pair[0], pair[1])));
  }
  
  auto structDef(string name, string[] members...) {
    return format!q{
      struct %s {
        %-(%s%)
      }
    }(name, memberDefs(members));
  }
  
  unittest {
    mixin (structDef("Foo", "int", "i", "double", "d"));
    auto f = Foo(42, 1.5);
    assert((f.i == 42) && (f.d == 1.5));
  }
  
  void main() {
  }