On Friday, 18 October 2013 at 06:13:38 UTC, DDD wrote:
I'm learning D. I'm curious about surprises I may get. I typically use C++, C# and javascript

Off the top of my head, I think the biggest one is that D doesn't offer "default constructors". Instead, it only has a default value initialization.

EG:

S s;
S s = S.init;
S s = S();
S s = S(5);

The first 3 lines are more or less equivalent. They will all simply initialize s to S.init, which is a compile time known value. S(5) will actually call a constructor.

This can bite you in the ass if you write something like:

struct S
{
    this(int i = 5)
    {}
}
S s = S(); //Does *not* call the constructor with the value 5.



Also, if you are coming from C++, know that D bans internal pointers. This is actually a sweet deal, because it means the D (unlike C++), is free to move objects around without ever having to explicitly duplicate them. It simply "bitcopies" the object, and never destroys the original. If you thought C++'s "move" semantics where sweet, they are nothing compared to D's straight up move.

Finally, D doesn't have "copy constructor". It has something a bit sweeter called "postblit". Basically, you first bitcopy the object you want to duplicate, and then, a function called "postblit" gets called, which does the work that is required (should any be required). What's nice about this is that the "target object" and "source object" never actually communicate.



Finally, if you are coming from C++, then "slices" will be new. It is important to understand what these do and don't do: http://dlang.org/d-array-article.html If you make the wrong assumptions about them, they *will* backfire on you.

Reply via email to