The daily D riddle

Timothee Cour thelastmammoth at gmail.com
Sun Jan 28 06:44:28 UTC 2018


why is `a.init` even legal? (instead of typeof(a).init)
likewise the following compiles, but IMO should not:
class A{ void fun(this a){}}
(instead we should have typeof(this)

How about deprecating these lax syntaxes?
they serve no purpose (we should use typeof(...)) and can cause harm
in generic code

On Sat, Jan 27, 2018 at 10:39 PM, Ali Çehreli via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> 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