The problem that took him 5 years to fix in C++, I solved in a minute with D

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Mar 10 20:25:12 UTC 2022


On Thu, Mar 10, 2022 at 10:47:51AM -0800, Ali Çehreli via Digitalmars-d wrote:
> On 3/10/22 06:35, matheus wrote:
> 
> > This video is from Jason and he is well known on the C++ community
> > and even gave some presentations on CppCon, so it was amusing seeing
> > him struggling with the language for something that I could do
> > easily in D (Again I'm not a D hardcore at all).

I used C++ extensively for many years before I switched to D.  I can say
from experience that metaprogramming takes off to a whole new level when
no special syntax is needed to do it. I.e., compile-time code is written
basically in the same way as runtime code (CTFE), no need to switch to
some DSL or sub-language dedicated for metaprogramming.  Instead of
convoluted shenanigans to navigate C++'s fractal minefield of misfit
quirks, you just write straight D code like you would any normal runtime
code, and it can be executed by CTFE without further ado. This is an
extremely powerful correspondence that makes metaprogramming as easy as
normal code, not some arcane black magic that only the initiated can
ever hope to approach.

You can see the same principle at work with C/C++'s macro system vs. D's
type-safe compile-time features.  Instead of having a separate
preprocessor complete with its own macro language for fiddling with the
program text, use the D language itself to describe and manipulate D
language structures (alias, static if, enums, templates, etc.). In fact,
marry alias, static if, etc., to CTFE, using basically the same syntax
as runtime D code (with minor distinctions), to get a well-integrated
whole that operates in all 3 domains at once.  Now *that's* empowerment.

Contrast that with the C++ situation where the preprocessor is a
separate sublanguage layered on top of the surface syntax, and
compile-time code needs constexpr littered all over the place with many
arbitrary limitations on what can/cannot be done, all distinct from
runtime code.  There's at least 3 separate sublanguages here with
artificial walls between them. No wonder it takes an expert 10 years to
navigate all the linguistic red tape just to get the simplest of tasks
done.

Now don't get me wrong, D isn't completely free of such artificial
boundaries either, but it's a lot less than in C++.  Metaprogramming in
D is a pleasant walk in the park, as opposed to a strenuous mind-racking
exercise in frustration.


[...]
> I am sorry C++ has so many bright people like Jason Turner in its
> grasp.  Jason Turner has been evangelizing "constexpr everything", not
> realizing that would make the keyword effectively a whitespace word,
> which would mean the need for the constexpr keyword is dubious. (Hint:
> D does not have such a keyword.) Despite appearances, C++ is a net
> loss to humanity. All those bright people could be spending their time
> actually solving engineering problems instead of finding brilliant
> workarounds to C++'s deficiencies.

It seems to be a common malady when working with C/C++: you spend LOT of
time fiddling with little details and wrestling with the language,
rather than making progress in your problem domain. I may be trying to
write a web client, for example, and about half of my time would be
spent not on solving issues and making progress with web programming,
but on fiddling with memory management, chasing down pointer bugs,
grappling with C++'s hideous template syntax, and avoiding language
minefields (double- and triple-checking my loop indices for off-by-1
errors, ad nauseaum).  But man it feels good when I finally overcome
these language barriers (ha!) and invent clever solutions to work around
them -- even though I may have hardly made any progress in my problem
domain (writing a web client).

Like a cool-looking futuristic power drill with hundreds of fancy knobs,
you spent 50% of your time tweaking this and turning that but after 2
hours you haven't begun to drill any holes yet.  Whereas the poor sod
next door uses this unpopular bland-looking drill and has already
finished his job before you've even begun yours.


> To respond to another point in this thread, I am not embarrassed to
> admit that I am emotional. I actually like to be emotional. Thank you
> very much!  :)

Rationality is overrated.  Human beings are more than just rational
thinking machines, emotions are an essential part of being human too.
:-P


[...]
> I regret even attempting to defend D's GC to enlightened (!) C++
> people. I used to be one myself. Now I simply solve problems with D
> and have fun doing it. The GC just works. How about redirecting some
> of the energy people put into C++ to learning different memory
> management strategies...

What about using a language where you don't have to flip bits on
magnetic media with a hand-held magnet (figuratively speaking), but
focus on, y'know, solving the problem you originally set out to solve?
:-D


[...]
> > PS: I'm a ESL
> 
> Me too.
> 
> > sorry for any English mistake.
> 
> You are doing everyone a favor by writing in English. Thank you! :)
[...]

Walter has said before, and I agree, that often ESL speakers communicate
much more clearly than native speakers who assume you would Just
Understand(tm), without making any effort to make themselves clear.
Ironically, the former apologize profusely for their clear
communication, but the latter feels no remorse for miscommunication.
;-)


T

-- 
"Real programmers can write assembly code in any language. :-)" -- Larry Wall


More information about the Digitalmars-d mailing list