Is D painted into a corner?
H. S. Teoh
hsteoh at qfbox.info
Fri Aug 29 02:48:21 UTC 2025
On Fri, Aug 29, 2025 at 01:52:59AM +0000, Lance Bachmeier via Digitalmars-d-learn wrote:
> On Thursday, 28 August 2025 at 18:47:19 UTC, Brother Bill wrote:
> > It seems like 'templates' are the 'Achilles heel' of D.
> >
> > Without starting a flame war, has D gotten to the point where
> > ordinary mortals have difficulty coding in D with 'templates' such
> > as 'cycle' requiring rewrites into 'myCycle'?
>
> Templates are in general a horrible way to program. Therefore I don't
> use them very much. When I do, I don't do anything complicated. But
> everyone has their opinion on that. I similarly don't use attributes.
> I prefer simple code with a simple language. Hard to avoid ranges in
> Phobos, which can be crazy complex/inconvenient at times, but every
> language has its warts.
[...]
Interesting how opinions differ on this. :-D I couldn't live without
templates. I might be tempted to quit D if I couldn't use templates...
;-) but OTOH there are times when templates are overused where they
aren't actually needed.
Once, I wrote an entire program using Phobos ranges, which was amazing
for fast prototyping thanks to pipeline programming. But it resulted in
hilariously huge generated symbols (like 4KB for a single symbol!) which
slowed compilation down to a crawl while the compiler soaked up RAM like
a sponge. Thankfully, after I complained about it in the forum Rainer
implemented a symbol compression scheme, which instantly reduced
compilation times back to normal levels.
Then later, while working on a different but somewhat related project, I
wanted to reuse the PNG generation modules I'd written entirely in
Phobos ranges. Performance was abysmal, though. So I ended up
rewriting it in more imperative-style code (well OK, with a couple of
ranges thrown in here or there 'cos I couldn't be bothered to rewrite
every last bit of perfectly fine, non-bottleneck code) and it worked
much better. Thanks to templates and DbI, the transition was smooth and
almost without hiccups. Without them, it would have been a painful
exercise in frustration re-fixing bugs that had already been previously
fixed.
Mind you, the new code still uses a fair share of templates, but with
just the performance-critical bits in more imperative style. A nice
balance between development effort and resulting performance, which is
something rarely achieved in other languages. In a low-level language
like C/C++ I'd be pulling my hair out in frustration as I try to
navigate language pitfalls and memory micromanagement galore; in a
high-level language like Java I'd be cursing the inability to get under
the hood and optimize a hot loop without jumping through hoops,
needlessly boxed native types, and mountains of boilerplate. In D?
Just a couple o' hours' worth of iteration with a mix of templated and
C-like code, and it's already good enough to do real work. Just a bit
more effort and it'd be ready for prime time. Best of both worlds!
T
--
The mathematician suffers from heart disease. He has an arrhythmatic problem.
More information about the Digitalmars-d-learn
mailing list