News for hackers. Stuff that matters.
Re:Idioms for the D Programming Language by nynx on Thursday February 11, 2021 @02:18PM (#26105864)
I'll be honest, D looks like a pretty inconsistent language. It has a lot of useful features for sure, but—having not used it—seemingly of the kitchen sink variety.
Re:Idioms for the D Programming Language by WalterBright on Thursday February 11, 2021 @03:06PM (#26106418)
With D it is unnecessary to build projects out of multiple languages, as D supports conventional C type programming, template metaprogramming, OOP, and functional style. (It even has an inline assembler for metal style!)

Each of those paradigms has its place, and I enjoy being able to mix and match them as appropriate in the same program.

Re:Idioms for the D Programming Language by crazypython on Thursday February 11, 2021 @08:15PM (#26109744)
You can even use opDispatch to generate methods or values of a class at compile time based on the name, Python __getitem__/JavaScript Proxy/Ruby style.
Re:Idioms for the D Programming Language by dunefox on Friday February 12, 2021 @06:20AM (#26112468)
That kind of sounds like a worse Common Lisp.
Re:Idioms for the D Programming Language by Foomf on Thursday February 11, 2021 @05:54PM (#26108554)
Its definitely of the kitchen sink variety. I don't think that's a bad thing though. D is a toolbox with a ton of tools in it. You might not need them all, but its nice that they're there when you do need them.

You're not forced to use the features you don't need.

Re:Idioms for the D Programming Language by lionkor on Friday February 12, 2021 @07:39AM (#26112989)
That's also what C++ is, but surprisingly people like to hate it for that, too :/
Re:Idioms for the D Programming Language by mhh__ on Thursday February 11, 2021 @02:24PM (#26105947)
Consistently pragmatic, is the philosophy, really.

There are some pain points that could probably go but realistically the aim is to make writing efficient code easy and abstract code even easier within the same language.

If you learn D you get to avoid a bunch of legalese compared to C++, and lots of runtime bugs compared to Python - there are more features to learn, but they generally make quite a lot of sense when you need them e.g. Ada-style contract programming is extremely useful the moment you don't have it.

Re:Idioms for the D Programming Language by einpoklum on Thursday February 11, 2021 @03:38PM (#26106815)
Can you point to somewhere which makes the case for D vs C++ as of 2020 or so?

I always had the sense - without seriously looking into it though - that D was about avoiding some of the mistakes/mis-features of C++, but C++ is always getting new stuff, which doesn't sit exactly well with what D has chosen, and then it plays somewhat clunky catch-up. But I may be very wrong.

Re:Idioms for the D Programming Language by wott on Thursday February 11, 2021 @06:02PM (#26108653)
> I always had the sense - without seriously looking into it though - that D was about avoiding some of the mistakes/mis-features of C++, [...] and then it plays somewhat clunky catch-up.

My opinion is that D lost its goal on the way. It was indeed supposed to be a cleaner C++ without all its confusing history of layers of features over layer of features. And it was so, for sometime. But then they started adding this, and this, and that, everything and all sorts of kitchen sinks depending on the fad of the year; meta-programming, taking from C++, or lately rustifying itself. The current state looks to me is as indigestible as modern C++ (and the process to get there has largely been the same, the process the original goal of D meant to rid and avoid). A pile of templates and annotations. D's evolution has been a great disappointment to me. I can see Nim going that way too.

Re:Idioms for the D Programming Language by einpoklum on Friday February 12, 2021 @05:18AM (#26112143)
But, you know, C++ can't be seen as a proper, complete language as it stood in 1998. A lot of it was partial scaffolding for future development of the language. In order to clean it, you either had to fill in the missing 25-years-or-so of C++ features (in some form or another), or settle on a much more limited language.
Re:Idioms for the D Programming Language by dunefox on Friday February 12, 2021 @06:21AM (#26112475)
It certainly looks very complex and feature heavy.
Re:Idioms for the D Programming Language by mhh__ on Thursday February 11, 2021 @04:12PM (#26107267)
C++ has improved a lot, but many of those solutions have been half-assed versions of things D does to completion.

CTFE in D works as you would expect, constexpr is a weird hack.

Concepts in C++ are ridiculously overcomplicated and are done with a much simpler solution in D.

Ranges are also much better thought out in D.

C++ will also never replicate D's ease of use when it comes to metaprogramming - static foreach and if for example have no alternative in C++ (if constexpr is incredibly stupid)

Re:Idioms for the D Programming Language by einpoklum on Thursday February 11, 2021 @05:52PM (#26108529)
So, you're saying: CTFEs, concepts and ranges done right. Hmm. Sounds good, actually...
Re:Idioms for the D Programming Language by mhh__ on Thursday February 11, 2021 @06:29PM (#26108938)
https://run.dlang.io/is/dkFWTu

Sink your teeth into that. Code generation at compile time, functional programming etc.

The compiler can actually check that this is pure for you but in this case it isn't performed so it can read from stdin

Re:Idioms for the D Programming Language by 1980phipsi on Thursday February 11, 2021 @08:07PM (#26109706)
D doesn't have concepts. It just takes a different approach.
Re:Idioms for the D Programming Language by lionkor on Friday February 12, 2021 @07:52AM (#26113092)
> if constexpr is incredibly stupid

Would you care to elaborate what's wrong with it? When I used it I always felt it was quite straightforward.

Re:Idioms for the D Programming Language by mhh__ on Friday February 12, 2021 @01:05PM (#26116566)
D CTFE restricts what you can't do. If I write my own vector type, even with my own malloc (it would have to get it's memory from the GC when running under CTFE for obvious reasons, but still), I can do useful work on it at compile time and then save that result in the data segment of my executable.

Constexpr tells you what you can do. It adds huge amounts of bulk to the standard, there is syntax soup everywhere now etc.

Re:Idioms for the D Programming Language by acehreli on Thursday February 11, 2021 @04:58PM (#26107939)
Yes, C++ has been catching up since C++11.

One thing C++ will never have is D's `static if`, which allows injecting declarations into scopes. Bjarne Stroustrup and others closed that door: https://isocpp.org/files/papers/n3613.pdf

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() {
  }
Re:Idioms for the D Programming Language by lionkor on Friday February 12, 2021 @07:41AM (#26113006)
Sounds like something `if constexpr ()` comes close to
Re:Idioms for the D Programming Language by p0nce on Friday February 12, 2021 @06:08AM (#26112401)
C++ doesn't implement the D features right.

For example C++20 has constexpr, consteval, constinit, for a less powerful equivalent of D's CTFE.

D use no syntax for this. Things are evaluated in CTFE when they can. The rules are simple, and you can ignore them for the first few years. It's because CTFE is considered a normal thing in D.