A D vs. Rust example

Quirin Schroll qs.il.paperinik at gmail.com
Thu Oct 27 17:37:59 UTC 2022


On Friday, 21 October 2022 at 15:13:44 UTC, Ola Fosheim Grøstad 
wrote:
> On Friday, 21 October 2022 at 13:53:28 UTC, Guillaume Piolat 
> wrote:
>> But building and agreeing on abstractions is what define the 
>> ecosystem.
>
> One big weakness in C++ and D is that generic definitions 
> cannot be checked without instantiation.

There aren’t many languages that have both, templates and 
generics; C++/CLI and C++/CX are the only ones I know and I 
happen to have some experience with them. (In this nomenclature: 
*template* = independent copy of the implementation for every 
distinct instantiation, cf. C++ or D templates, especially class 
templates with mutable static variables; *generic* = one 
implementation, cf. Java or C#, and static variables cannot 
depend on the argument types.)

D could introduce generics and it would actually profit more from 
them than e.g. C++ would because D has first-class reference 
types and almost no user-defined implicit conversions. Templates 
are not really a replacement for generics. As an example where 
generics (would) shine and templates fall flat, consider [range 
interfaces](https://dlang.org/phobos/std_range_interfaces.html). 
We have (among others) `InputRange` and `BidirectionalRange`. A 
function taking an `InputRange!Base` parameter should allow for 
an argument of type `BidirectionalRange!Derived` (with `Derived` 
and `Base` classes or interfaces). In D, that is not possible 
because we cannot express the 
[variance](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)) of a type parameter; templates are inherently antithetical to that.

Imagine how much more attractive D would be to OO crowd if it had 
first-class value type boxing, generics with variance and 
wildcards, and all the other OO stuff modern OO-fist languages 
offer. Using Java and C#, it feels like D only has the bare 
minimum to call itself object-oriented. There was some talk (by 
Átila, IIRC) that having first-class classes and OO in D was a 
design error. I have no idea whether there is actually a plan to 
fade those out improve them to be competitive on that front.


More information about the Digitalmars-d mailing list