The daily D riddle

Ali Çehreli acehreli at yahoo.com
Sun Jan 28 06:39:05 UTC 2018


On 01/27/2018 10:25 PM, Shachar Shemesh wrote:
> What will the following code print? Do not use the compiler:
> 
> import std.stdio;
> 
> struct A {
>      int a = 1;
> 
>      void initialize() {
>          a = a.init;
>      }
> }
> 
> void main() {
>      A a;
>      a.initialize();
> 
>      writeln(a.a);
> }
> 
> I find this behavior unexpected.

I used the compiler to check my guess and I was wrong. The following 
makes the difference:

         a = A.init.a;

So we currently have:

     a.init          (type's init value)
     A.init.a        (members' init value)

If it were designed as you want, we would have the following:

     typeof(a).init  (type's init value)
     a.init          (members init value)

Well, too late I guess. :)

Ali


More information about the Digitalmars-d mailing list