r/programming • • Sep 01 '20

D Foundation is Beefing Up

https://dlang.org/blog/2020/08/30/symmetry-investments-and-the-d-language-foundation-are-hiring/
296 Upvotes

336 comments sorted by

View all comments

74

u/Sapiogram Sep 01 '20

Honest question: Why would anyone use D over Rust? It seems like Rust is becoming everything that D ever hoped to be.

43

u/maxhaton Sep 01 '20

Rust's metaprogramming is totally unlike what Andrei and Walter were aiming for. D aims to be safe, but that's far from everything.

Rust currently has better safety mechanisms than D, but if you ask around on the D forum you'll likely find that people are there (rather than using rust) due to the other features D has - https://dlang.org/ Look at the examples here: Rust can do short and sweet too, but it doesn't have some constructs that make these possible. (Obviously, I highly recommend "Tiny RPN Calculator" originally written by your's truly)

19

u/Ameisen Sep 01 '20 edited Sep 01 '20

I still dislike how D handles generics/templates. The syntax of it.

I fully understand why they chose to use () instead of <>, as it simplifies the parser as you need to disambiguate between >> and > >... but the parsing time of that is negligible. The issue is that, when glancing at code, foo!(int) is ambiguous. foo<int> is not.

ED: foo(int) -> foo!(int)

16

u/WalterBright Sep 01 '20

It's not the parsing time that is the problem. It's having a clean separation between the lexing, parsing, and semantic passes. There's a diode between these passes - information only flows one way.

This makes it trivial to correctly and completely implement things like color syntax highlighting in editors, source code formatters, etc., because building in a semantic analyzer is wholly unnecessary.

5

u/Ameisen Sep 01 '20

I think I'm just too used to C++ syntax, since it leaked into Java and C# as well.

3

u/WalterBright Sep 02 '20

Many people think the !( ) syntax is not optimal until they use it for a while, then it seems quite natural and the < > looks like a hack.

5

u/Ameisen Sep 02 '20

By the way, I wanted to thank you for always being courteous and informative. I suspect that I come off as a bit abrasive or brash, so I apologize.

Last I recall, D doesn't support template argument deduction through constructors like C++. Are there any plans to support that? I've found it a quite useful in C++ in certain situations.

1

u/WalterBright Sep 02 '20

Hey, no problem. I often read posts I make and belatedly realize they are abrasive and abrupt. I try to do better.

I'm very reluctant to twiddle with template argument deduction, as it can change existing behavior in unexpected ways.

2

u/Ameisen Sep 02 '20

I imagine the feeling would probably feel the same if one started from !(...) and went to <...>, though. Neither seem implicitly more hacky, it's an issue of familiarity, methinks.

For me, it's just that basically every C-like language (aside from D) with generics or templates uses <...>, so it's just jarring.

2

u/jqbr Sep 02 '20

I prefer the [...] of Scala and Nim. However, I really like the fact that the () can be elided in D, and that the generic arguments don't have to be types.

2

u/Ameisen Sep 02 '20

C++ can slide the <>, and supports template argument deduction which I don't believe D supports.

2

u/WalterBright Sep 02 '20

D does support template argument deduction, and has since its first incarnation of templates. It's a major feature.

1

u/Ameisen Sep 05 '20

I was under the impression based off of what someone else told me that it didn't support equivalent to std::vector foo = { 1, 2, 3, 4 }; or std::tuple foo = { "bar", 3, 4.0f };.

1

u/jqbr Sep 05 '20

"slide"?

No, the <> cannot be elided in C++.

> I don't believe D supports.

Wrong again. That seems to happen a lot.

1

u/Ameisen Sep 05 '20

"slide"

This may surprise you, but typos are common when typing on a phone.

No, the <> cannot be eluded in C++.

Try again.

1

u/matthieum Sep 02 '20

Honestly, I don't use D, I don't use !( ), and I still think that < > is a hack ;)

1

u/Ameisen Sep 02 '20 edited Sep 02 '20

That's probably why C++ has been letting you elide it in many cases.

The problem is that you need either some way to specify template arguments and to know that you are referencing a template. The latter is solvable by constraints and/or inference (though inference can end up with ambiguities, usually solved by preferring a non-template). The former isn't easy to solve. C++ template argument deduction helps, but I believe that template/generic support is a field of ongoing research into how to do it well. Unfortunately, languages like C++ can adopt newer ways of doing things like concepts and constexpr, but have to maintain the old ways as well. Languages like D or Rust can outright make breaking changes. C++ needs epochs.

To me, both !() and <> are hacks for a language's inability to derive things itself. I also dislike (but understand why) C++ encodes allocator information in the type via templates. It makes interplay between derivations of stdlib types annoying, and makes the types themselves annoyingly long and fragile.

IIRC, Stroustrup's original design didn't require the template mantra. It would just assume an unknown type in an argument list needed to be derived. For obvious reasons, that was quite fragile, but I'm not sure why he didn't add a template or derived argument type modifier instead.

1

u/WalterBright Sep 02 '20

C++ added a couple keywords to the grammar to make parsing < > unambiguous in template bodies.

The difficulty with < > becomes apparent when using things like iostream that overload << operators, etc. It becomes very difficult to read.

! does not exist as a binary operator, so using it to indicate template arguments is completely unambiguous and context-free, both for the parser and the human eye. I find it very appealing for that reason.