Warnings/What should I know?

Maxim Fomin maxim at maxim-fomin.ru
Fri Oct 18 00:41:28 PDT 2013


On Friday, 18 October 2013 at 06:56:49 UTC, monarch_dodra wrote:
> 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.

Not always actually.

>
> 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.
>

One can bite in the ass even more in case of

struct S { @disable this(); } and using S.init or

void foo () { int i; struct S { int bar() { return i; } } and 
also using S.init

>
>
> 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.
>

How much are there threads asking what is wrong with the language 
when there are const/immutable structs and postblit :) ?




More information about the Digitalmars-d-learn mailing list