The daily D riddle

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Jan 28 06:44:40 UTC 2018


On Sunday, January 28, 2018 08:25:51 Shachar Shemesh via Digitalmars-d 
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.

It does exactly what I'd expect it to do, though honestly, it's the sort of
thing I wish weren't legal, just like I wish that it weren't legal to call a
static member function via a member. Maybe there are cases where it's
useful, but it just seems wrong.

In any case, init goes with a type, not a variable, which is why it acts the
way it does.

- Jonathan M Davis



More information about the Digitalmars-d mailing list