Autodecode?
Steven Schveighoffer
schveiguy at gmail.com
Sun Aug 16 22:20:40 UTC 2020
On 8/16/20 4:53 PM, JN wrote:
> Related to this thread:
> https://forum.dlang.org/post/xtjzhkvszdiwvrmryubq@forum.dlang.org
>
> I don't want to hijack it with my newbie questions. What is autodecode
> and why is it such a big deal? From what I've seen it's related to
> handling Unicode characters? And D has the wrong defaults?
Aside from what others have said, autodecode isn't really terrible as a
default. But what IS terrible is the inconsistency.
Phobos says char[] is not an array, but the language does.
e.g.:
char[] example;
static assert(!hasLength!(typeof(example))); // Phobos: no length here!
auto l = example.length; // dlang: um... yes, there is.
static assert(!hasIndexing!(typeof(example))); // P: no index support!
auto e = example[0]; // D: yes, you can index.
And probably my favorite WTF:
static assert(is(ElementType!(typeof(example)) == dchar)); // P: char is
a range of dchar!
foreach(e; example) static assert(is(typeof(e)) == char)); // D: nope,
it's an array of char
This leads to all kinds of fun stuff. Like try chaining together several
char[] arrays, and then converting the result into an array. Surprise!
it's a dchar[], and you just wasted a bunch of CPU cycles decoding them,
not to mention the RAM to store it.
But then Phobos, as it's telling you that all these things aren't true,
then goes behind your back and implements all kinds of special cases to
deal with these narrow strings *using the array interfaces* because it
performs better.
We will be much better off to be done with autodecoding. And for many
cases, autodecoding is just fine! Most of the time, you only care about
the entire string and not what it's made of. Many other languages do
"autodecoding", and in fact the string type is opaque. But then it gives
you ways to use it that aren't silly (like concatenating 2 strings knows
what the underlying types are and figures out the most efficient way
possible). If `string` was a custom type and not an array, we probably
wouldn't have so many issues with it.
-Steve
More information about the Digitalmars-d-learn
mailing list