A D vs. Rust example
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Thu Oct 27 18:43:14 UTC 2022
On Thursday, 27 October 2022 at 17:37:59 UTC, Quirin Schroll
wrote:
> 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.
I have found that it is to some extent possible to do such type
checks of generics with C++ concepts. I've made some attempts for
a generic graphics library I've started on, but it gets tedious
quite fast. I would imagine you could do some of the same in D in
a more ad-hoc matter, but that would be even more tedious.
It basically involves setting up all the constraints and then
test them with a single static assert (which involves
instantiation, but in the definition file without explicitly
using the instance methods).
The C++ concept-feature is helpful in this regard, but not
helpful enough to make it pleasant… which basically limits my
eagerness to use of it in practice. Still, a move in the right
direction.
D has a chance to learn from C++ and create a better
concept-feature. Don't replicate it, look at the use cases where
C++ concepts become tedious or difficult to get right, then
design something better.
> 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.
This is probably true, but you could instead try to find a way to
clearly specify and statically check template requirements.
> 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.
That is a reasonable angle given the large number of programmers
that have experience with Java and C#, but I think D could take a
closer look at how templates are done and find out what is
missing. When was the last time a feature was added to support
generic programming?
> 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.
OO is not a mistake, I agree. I find it quite frustrating to not
have inheritance when programming in Go. Just basic things like
having a linked list node in the super class makes things much
simpler/cost-efficient when you are mixing types in
data-structures.
More information about the Digitalmars-d
mailing list