@nogc and Phobos

H. S. Teoh hsteoh at qfbox.info
Sat Mar 11 21:07:18 UTC 2023


On Sat, Mar 11, 2023 at 04:21:40PM +0000, bomat via Digitalmars-d-learn wrote:
[...]
> Although I come from a C++ background, I'm not exactly a fanboy of
> that language (you can probably tell, otherwise I wouldn't be here).
> But after hearing praise for D for being a cleaner and better version
> of C/C++, I am a bit disappointed so far, tbh. I don't want to go into
> too much detail here to not derail the thread entirely, but I think it
> repeats too many old sins, like implicit type conversions, the `for`
> loop syntax (although I guess one wouldn't need it that often because
> of `foreach`), the `switch` `case` fallthrough, and the cancerous
> `const` (as far as I can tell, `immutable` is an even worse flavor of
> it).
[...]

I also came from a C/C++ background.  The GC turned me off D for a long
time, until one day I decided to just give it a try to see if it was all
as bad as people made it sound.  I have to admit that GC phobia stuck
with me for a long time, but once I actually started using the language
seriously, I discovered to my surprise that it wasn't *that* big of a
deal as I had thought. In fact, I found that I quite liked it, because
it made my APIs cleaner. A LOT cleaner because I didn't have to pollute
every function call with memory management paraphrenalia; they can be
nice and clean with no extraneous detritus and things Just Work(tm).
Also, the amount of time/effort spent (i.e., wasted) debugging memory
problems was gone, and I was a LOT more productive than I ever was in
C++.  True, I have to relinquish 100% control of my memory, and as an
ex-C++ fanboy I totally understand that it's not a pleasant feeling. But
I have to say that I was pleasantly surprised at how much D's GC
*didn't* get in my way, once I actually started using it for real (toy
examples can be misleading).

Why am I saying all this?  Because to be frank, you haven't really used
D if you've been avoiding its standard library like the plague. Not all
of Phobos is GC-dependent; the range-based stuff, for example, lets you
avoid GC use most of the time. True, for exceptions you need GC, but
exceptions are supposed to be ... exceptions ... not the norm, and in
practice it isn't really *that* big of a deal. You shouldn't be catching
exceptions inside performance-sensitive inner loops anyway.  D's strong
points don't really show until you start using range-based stuff with
UFCS chains -- now *that's* power.  Even if you dislike the GC you can
still mostly manage your own memory, and let the GC be the fallback
mechanism for stuff you missed.

As for implicit type conversions: I don't know where you got your
information from, but D's implicit conversions are a WHOLE different
world from C++. Walter has resisted adding implicit conversion
mechanisms in spite of harsh criticism and constant pressure, and in
practice, you aren't gonna see a lot of it in D code, if at all. It's
not even close to C++ where SFINAE + Koenig lookup gang up on you from
behind and you don't even know what hit you. Issues with implicit
conversions in D only really come up if you go out of your way to abuse
alias this and/or use short ints a little too zealously. Otherwise in
practice it's not even an issue IME.

For-loop syntax: I can't remember the last time I wrote one in D. Maybe
I did like 1 or 2 times (across my 20+ D projects) when I really needed
to do something weird with my loops. But foreach covers 90% of my
looping needs, and while loops take care of the 9.9% of the cases.
Besides, once you start getting used to UFCS chains and Phobos
algorithms, most of the time you won't even be writing any loops at all.
You won't believe how much more readable your code becomes when you can
finally stop worrying about pesky fragile loop conditions and just tack
on a couple more components to your UFCS chain and it just automagically
takes care of itself.  Again, not something you'll understand if you
never tried to use D in a serious way. I recommend actually trying to
write D, not as transplanted C++, but the way D code is meant to be
written.

As for switch: yeah D switch has some crazy parts (like Duff's device --
you can actually write that in D). But I've never needed to use it...
also, final switch + enums = awesome.

As for const: I hardly ever use it. It's useful occasional for low-level
code, but not much beyond that.  My advice: don't bother. Just pretend
it doesn't exist, and your life will be happier. Well OK, once in a
while you do need to deal with it. But if it were me, I'd avoid it
unless I have to.  It doesn't mix well with high-level code, I'll put it
that way. Immutable is the same thing, I only use it as `static
immutable` just so the compiler would put my data in the preinitialized
segment. Other than that, I don't bother.


T

-- 
If you're not part of the solution, you're part of the precipitate.


More information about the Digitalmars-d-learn mailing list