My thoughts & experiences with D so far, as a novice D coder

Vidar Wahlberg vidar.wahlberg at gmail.com
Wed Mar 27 08:34:19 PDT 2013


I know I'm probably going to upset some people with this, bashing 
their favourite child and all, but I wanted to let you know the 
experience I've had with D so far, as a novice D coder with a 
heavy Java & light C++ background.
It's not that I dislike D, in fact there are tons of things I 
love about it, it's pretty much exactly what I'm looking for in a 
programming language at the moment. Yet, I encounter some 
frustrating issues when coding, often leaving me with the 
impression that I'm fighting the language more than the problem 
I'm trying to solve.
True, there are many things I don't know about D, compilers or 
the inner workings of a computer, and some of the fights I have 
with the language are likely started by myself because I'm 
dragging along my bias from other languages, drawing 
misconceptions on how the D language actually works.
My intentions are not to insult, but shed some light on some of 
the difficulties I've faced (and how I solved them), with the 
hope that it will help others from facing the same difficulties.


Woes:
-----
- I find myself in a world of pain when I want to share data more 
complex than the basic data types (int, char, byte, etc) across 
threads. Seemingly the magic trick is to "cast(shared) foo" (or 
"cast(immutable)") when passing objects/references to another 
thread, then "cast(Foo)" back on the receiving end (as most 
classes/structs in the standard library refuse to let you call 
any methods when the object is shared). The examples in the 
source and TDPL are fairly limited on the issue, it mostly covers 
only those basic data types.

- While the "auto"-keyword often is great, it can lead to 
difficulties, especially when used as the return type of a 
function, such as "auto foo() { return bar; }". Sometimes you may 
wish to store the result of a function/method call as a global 
variable/class member, but when the function/method returns 
"auto" it's not apparent what the data type may be. While you may 
be able to find out what "bar" is by digging in the source code, 
it can still be difficult to find. One example is to save the 
result of "std.regex.match()" as a member in a class. For me the 
solution was to "import std.traits", create a function "auto 
matchText(string text) { return match(text, myRegex); }" and 
define the class member as "ReturnType!matchText matchResult;" 
(do also note that function & member must come in the right order 
for this to compile). This was all but obvious to a novice D 
coder as myself, the solution was suggested to me in the IRC 
channel.


Gotchas:
--------
- The lack of "rectangular" arrays created at runtime in D ("int 
i = 5; int[i][i] foo;") can be quite confusing for programmers 
with Java or C++ background. Even though there exists 
alternatives 
(http://denis-sh.github.com/phobos-additions/unstd.multidimensionalarray.html), 
this design decision and how to get around it when you really 
desire a "rectangular" array could be explained in more detail at 
http://dlang.org/arrays.html.

- Static array versus dynamic array was one of the first traps I 
stepped on 
(http://forum.dlang.org/thread/jnu1an$rjr$1@digitalmars.com). 
Until Jonathan M. Davis explained it in detail 
(http://d.puremagic.com/issues/show_bug.cgi?id=8026#c4), I pretty 
much considered it as "magic" that "randomShuffle(staticArray);" 
did not sort the array while "randomShuffle(staticArray[]);" did 
(the first call now gives you an compile error, though). That 
static arrays are value types while dynamic arrays are reference 
types may not be obvious for those with primarily Java background.

- When casting a value to an enum, there's no checking that the 
value actually is a valid enum value. Don't think I ever found a 
solution on how to check whether the value after casting is a 
valid enum value, it hasn't been a pressing issue.

- Compiling where DMD can't find all modules cause a rather 
cryptic error message. A solution is to make sure you specify all 
source files when compiling.


Wishlist:
---------
- "void[T]" associative array (i.e. a "set") would be nice, can 
be achieved with "byte[0][T]".

- "Foo foo = new Foo();" for global variables/class members. Now 
you must "Foo foo; static this() { foo = new Foo(); }".


More information about the Digitalmars-d mailing list