DIP 1027---String Interpolation---Format Assessment

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Feb 27 20:00:52 UTC 2020

On Thu, Feb 27, 2020 at 11:26:37AM -0800, Walter Bright via Digitalmars-d-announce wrote:
> Magic types are not simple and inevitably lead to unexpected corners
> and unresolvable problems. *cough* associative arrays *cough*

For all the trouble they've given us, built-in AA's is one of the
primary reasons I love D.

IMO, it's inexcusible for a programming language in this day and age not
to have built-in AA's, either as part of the language, or as part of the
standard library -- but with FULL LANGUAGE SUPPORT.  Meaning that all
built-in types can be used as AA keys without further ado, unlike C++'s
standard hashtable which IMNSHO is a horrible piece of nigh-unusable
crap, because you can't even declare a hashtable without defining your
own hash function, you can't use structs as keys unless you also
manually declare a hash function, a comparison function, and who knows
what other onerous detail I can't recall, and then you have to use that
horrendous iterator API to lookup anything at all.

Once, I wanted to add a simple hashtable as a caching mechanism to one
of my C++ projects. After *two days* it still wasn't working properly,
because there is no default hash function for anything, not even ints,
so I have to write my own, and the hash function must be wrapped in some
kind of functional wrapper object because you can't pass functions to
the template parameter, then to initialize the hashtable I have to jump
through a whole bunch of hoops, mostly involving the fact that to create
the hash function object I needed to pass the necessary context all over
the place -- so I had to rewrite 70% of the internal API just to be able
to pass the context to the right places. Then the dumb thing required
C++11 or something like that, which broke my C++98 code in horrendous
ways that required a full-scale refactor to fix some of the

Eventually I threw in the towel, and spent about a month's time to
rewrite the whole danged project in D -- from scratch. I can't describe
how refreshing it was to be able to just write:

	Value[Key] aa;

and that's *it*.  No fuss, no muss, it Just Works(tm).  Sure, when
there's a performance issue then I have to add opHash or opEquals or
what-not to my types, but the important thing is, there are *sane
defaults* for everything.  The compiler isn't going to throw my code
back in my face just because I used a custom type instead of string for
the AA key (some languages have this problem), and the custom type may
contain other custom types but it just knows to compute the default hash
value accordingly.

If D *didn't* have .opHash built-in, it would've been a totally
different story, probably not too unlike my horrible experience with
C++.  If there wasn't a default hash function, and if I had to manually
declare a whole bunch of stuff just to be able to use a struct
containing a pair of ints as key, then it would have been a horrendous

TL;DR: I'm *glad* D has built-in AA's, in spite of whatever flaws they
may have.  It was the Right Choice, even though the execution, in
retrospect, was a bit wanting.


MSDOS = MicroSoft's Denial Of Service

More information about the Digitalmars-d-announce mailing list